开发者

\s++ Regular Expression

开发者 https://www.devze.com 2023-01-24 07:10 出处:网络
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.

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

精彩评论

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