For a long time I've been using preg_replace_callback to replace any occurrence of a reference ID into a link that opens the reference.
For example, "BIZ1234" would be replaced with Name title (pseudo code)
All good.
But now I need to not do the replacing if a backslash precedes the match.
For example, if the "BIZ1234" is used in a folder path: \drive_C\BIZ1234
For this I've tried a lot of regular expressions, and almost got what I needed, but my skills in regular expressions just aren't enough.
Current regular expression:
/BIZ(\d+)/i
Attempts of excluding occurrences where a backslash precedes the match:
/[^\\\\]BIZ(\d+)/i
This works if the backslash is right infront of BIZ. I realize I have to set some kind of limit as to how far back the backslash can be. So I try:
/[^\\\\](.{0,20})BIZ(\d+)/i
This replaces too much and doesn't care about the backslash.
I've read and read, but just can't grasp these regular expressions. Help! :-)
EDIT / ADDED:
I'm using the following as a test string (the following is 1 string):
<p>This should become a link BIZ1234</p>
<p>this/here/should/too BIZ1234</p>
<p>but\this\here\shouldn't BIZ1234</p>
Currently, using this regular expression:
/^([^\\\]+)BIZ(\d+)/
Replaces nothing in the above string.
As a lookbehind alternative, could use \K to reset the beginning of the match:
$pattern = '/>[^<\\\]*\KBIZ(\d+)/';
This would replace only from where you put the \K
.
So for example match >
followed by [^<\\\]*
any amount of characters that are NOT <
or backslash, followed by BIZ
but reset and start replace just before BIZ
.