php - PCRE regular expression overlapping matches -
i have following string
001110000100001100001 and expression
/[1]....[1]/g this makes 2 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
$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
Post a Comment