php - PCRE regular expression overlapping matches -


i have following string

001110000100001100001 

and expression

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

this makes 2 matches

matches

but want match pattern between both lookbehind say, overlapping 1

i have absolutely no clue, how can work ? instead of 0 can characters

a common trick use capturing technique inside unanchored positive lookahead. use regex preg_match_all:

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

see regex demo

the values in $matches[1]:

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

see lookahead reference:

lookaround matches characters, gives match, returning result: match or no match. why called "assertions". not consume characters in string, assert whether match possible or not.

if want store match of regex inside lookahead, have put capturing parentheses around regex inside lookahead, this: (?=(regex)).


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -