Page 1 of 1

Help this Neophyte learn REGEX

Posted: Sat Jan 22, 2005 9:08 pm
by neophyte
I'm finally getting around to tackling REGEX.... Of all the things I've learned about PHP, REGEX was never on the top of my list. Anyway -- I got this little snippet of code to work fine -- basically, I wanted it to remove the whole string including the comment marks. Then I added the extra characters $%#^ etc. and can't get it to work. Does somebody know how I would change this REGEX to replace the entire $String?

Code: Select all

$string = "<!--THIS TEXT I WA#$%^@NT OUT OF HERE-->JHGFJTHFHT";
$clean_string = preg_replace("/<!--&#1111;a-z0-9]\s+-->/i", "", $string);
echo $clean_string;

Posted: Sat Jan 22, 2005 9:15 pm
by feyd
pattern:

Code: Select all

#<!--.*?--\s*>#
edit: fixed to HTML 4.01 W3C compliant.

Posted: Sat Jan 22, 2005 9:17 pm
by Cronikeys
I am just curious how you knew that? I mean how is that determined... perhaps point me in the direction of a tutorial?

Posted: Sat Jan 22, 2005 9:20 pm
by neophyte
Feyd is the King of REGEX. I've seen him come up with REGEX for things I never though possible. It makes me wonder if he's using REGEX coach or something like that....

Tutorials? try http://www.hudzilla.org

Great place to learn PHP. Including REGEX of course! :D

Posted: Sat Jan 22, 2005 9:20 pm
by feyd
I don't know of any great tutorials on regular expression, as they are fairly complex to begin with. I can recommend getting the regex bible: Mastering Regular Expressions (O'Reilly Associates)

I learned regex mostly through experimenation, but since getting the bible, I grew quite a bit in my understanding and limitations on more advanced bits like lookbacks..