PCRE Regular Expression Matches

I have the following line

001110000100001100001 

and this expression

 /[1]....[1]/g 

it makes two matches

matches

but I want it to also match the pattern between both with lookbehind, so to speak, overlapping 1

I have absolutely no clue how this might work? instead of 0 can be any characters

+5
source share
1 answer

A common trick is to use the capture technique inside an unexplored positive look. Use this regex with preg_match_all :

 (?=(1....1)) 

Watch the regex demo

Values are in $matches[1] :

 $re = "/(?=(1....1))/"; $str = "001110000100001100001"; preg_match_all($re, $str, $matches); print_r($matches[1]); 

See the reference guide :

Lookaround actually matches the characters, but then discards the match, returning only the result: match or no match. That is why they are called "statements." They do not consume characters in a string, but only claim whether a match is possible.

If you want to keep the regex match inside the lookahead , you need to place the brackets around the regex inside the lookahead , for example: (?=(regex)) ,

+5
source

Source: https://habr.com/ru/post/1243224/


All Articles