My pattern works in JavaScript.
(?<=(?:username|Email|URL)(?:\s|:))[^\n]+
However, when I try to use it in PHP, I get this error:
A lookbehind assertion has to be fixed width
How I can fix it?
Use a full string match restart (\K
) instead of the invalid variable-length lookbehind.
/^(?:username|Email|Url):? *\K\V+/mi
Make the colon and space optional by trailing them with ?
or *
.
Use \V+
to match the remaining non-vertical (such as \r
and \n
) characters excluding in the line.
See the broader canonical: Variable-length lookbehind-assertion alternatives for regular expressions
To protect your script from falsely matching values instead of matching labels, notice the use ^
with the m
modifier. This will ensure that you are matching labels that occur at the start of a line.
Without a start of line anchor, Somethingelse: url whoops
will match whoops
.
To make multiple matches in PHP, the g
pattern modifier is not used. Instead, apply the pattern in preg_match_all()