Page 1 of 1
RegEx problem
Posted: Thu Feb 04, 2010 2:17 pm
by regexnewbie
Hello,
I'm a RegEx newbie and am struggling to understand some code that I've inherited. I was hoping someone could break this down for me to let me know what is going on here:
preg_replace('/(.*)(<\?\s*?include\s*?\(\s*[\'"].*?\/incs\/.*?[\'"]\s*\)[^>]+?\?>)(.*?)(<\/td>)(.*)/s','$1$2$4$5',$txt);
In addition to wondering what this does, my return string is always empty...shouldn't it return the original even if there isn't a match?
Thanks for all your help with this.
Re: RegEx problem
Posted: Thu Feb 04, 2010 2:56 pm
by AbraCadaver
regexnewbie wrote:Hello,
I'm a RegEx newbie and am struggling to understand some code that I've inherited. I was hoping someone could break this down for me to let me know what is going on here:
preg_replace('/(.*)(<\?\s*?include\s*?\(\s*[\'"].*?\/incs\/.*?[\'"]\s*\)[^>]+?\?>)(.*?)(<\/td>)(.*)/s','$1$2$4$5',$txt);
In addition to wondering what this does, my return string is always empty...shouldn't it return the original even if there isn't a match?
Thanks for all your help with this.
I'll give it a try (simplified):
Code: Select all
/ just a starting delimiter to delimit the pattern
(.*) any number of any characters capture as backreference $1
(<\? followed by <? - start capturing as backreference $2
\s*? followed by any number of whitespace characters
include followed by the literal string "include"
\s*? followed by any number of whitespace characters
\( followed by a (
\s* followed by any number of whitespace characters
[\\'"] followed by 1 single or double quote
.*? followed by any number of any characters
\/ followed by /
incs followed by the literal string "incs"
\/ followed by /
.*? followed by any number of any characters
[\\'"] followed by 1 single or double quote
\s* followed by any number of whitespace characters
\) followed by a )
[^>]+ followed by 1 or more characters that are NOT a >
\?\?>) followed by ??> - end capturing as backreference $2
(.*?) followed by number of any characters capture as backreference $3
(<\/td>) followed by </td> capture as backreference $4
(.*) followed by any number of any characters capture as backreference $5
/ ending delimiter
s modifier to make the . match newline characters
Assuming that $txt contains something, then the following code would return the replaced version or the original string if there were no replacements or null if there was an error:
Code: Select all
$txt = preg_replace('/(.*)(<\?\s*?include\s*?\(\s*[\'"].*?\/incs\/.*?[\'"]\s*\)[^>]+?\?>)(.*?)(<\/td>)(.*)/s','$1$2$4$5',$txt);