I am trying to parse a string with something like :
preg_match( "|from:(.*?);|", $string, $match);
But then I found that the string can also contain lfrom: and _from:
A few examples of how the string can be:
var1:34234;from:website1.com;lfrom:website2.com;var2:343423;
lfrom:website1.com;var1:4234234;from:website2.com
from:website1.com;_from:website2.com;lfrom:website2.com;var1:43523;
How can I parse only from:(.*?); and not lfrom, _from, etc.
I was gonna give you the solution but I better explain you about the lookbehind modifier.
In regex each time you “match” a
hfor example, thathwill add 1 to the pointer of where the regex is at the moment so you dont want to “add” nothing to the pointer. You just want to look if thefromis preceded by a;\s\bor the start of the string. You don’t want to match the VOID because there are voids everywhere!!So, an example:
(?<a)bthat would match abthat has anabefore it. So it just does the next: When abfound it looks before it, if there is anait matches the regex.So…
(?<=[;\s\b]|^)from:(\w+\.\w+)Would match afromthat right before it has[;\s\b] OR ^ (The string start)DEMO
Pretty easy, huh!?