开发者

PHP: preg_match regex not finding correct strings

开发者 https://www.devze.com 2023-01-17 04:18 出处:网络
preg_match_all(\'/[\\s]{1}(AA|BB|CC)+[\\s]{1}/\',\' AA BB \',$matches); result is AA, but I n开发者_JS百科eed AA and BB.The [\\s]{1} sequences* you\'re using to match whitespace overlap between the
preg_match_all('/[\s]{1}(AA|BB|CC)+[\s]{1}/',' AA BB ',$matches);

result is AA, but I n开发者_JS百科eed AA and BB.


The [\s]{1} sequences* you're using to match whitespace overlap between the matches. The trailing space after "AA " is the same space as the one before " BB". Any one character can only be matched a single time, so after the scan finds " AA " it only searches the remaining "BB " string for a match, and fails to find one.

Try the word boundary escape sequence \b instead. This matches the beginnings and ends of words but does not actually consume any characters, so it can match multiple times:

preg_match_all('/\b(AA|BB|CC)+\b/', 'AA BB', $matches);

Using \b has the bonus effect of not requiring the extra spaces you had surrounding your string. You can just pass in 'AA BB' instead of ' AA BB ' if you wish.

* By the way, [\s]{1} is the same thing as [\s], which is the same as a simple \s. No need for the square or curly brackets.


The problem is you are trying to match the same space twice. Using a look ahead (?=\s) should help:

preg_match_all('/\s(AA|BB|CC)(?=\s)/',' AA BB CC BB AA ',$matches);


You can do a positive look-behind:

/(?<=\s)(AA|BB|CC)+\s/


Resources :

  • regular-expressions.info - lookbehind
0

精彩评论

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