开发者

\s++ Regular Expression

I've seen \s++ but I'm not sure what it mean开发者_如何学Cs. Could you please use an example? (\s means white space)


\s++ means to match one or more whitespace characters possessively.

It’s the same as writing (?>\s+). The extra plus makes it possessive, like an independent subgroup.

This illustrates the difference:

% perl -Mre=debug -le 'print (("a " . " " x 300 . "x") =~ /a\s+\d/ || 0)' | & wc -l
     621
% perl -Mre=debug -le 'print (("a " . " " x 300 . "x") =~ /a\s++\d/ || 0)' | & wc -l
      26

I’m counting how many steps the regex engine takes to solve the match. There isn’t a match though, but it doesn’t know that. It tries a bunch of real stupid things in the first case that the possessive match prevents it from trying in the second. Once a possessive match has been made, it cannot be backtracked into and recalculated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜