RegEx problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
regexnewbie
Forum Newbie
Posts: 1
Joined: Thu Feb 04, 2010 2:15 pm

RegEx problem

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: RegEx problem

Post 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);
 
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply