in reply to Re: Eugh, regex :(
in thread Eugh, regex :(

Hi,

All this is aiming to do, is change stuff like:

[URL=http://img207.imageshack.us/my.php?image=dsc03598vt7.jpg][IMG]http://img207.imageshack.us/img207/2964/dsc03598vt7.jpg[/IMG][/URL]

..to:

[IMG]http://img207.imageshack.us/img207/2964/dsc03598vt7.jpg[/IMG]

(i.e remove the URL stuff)

Cheers

Andy

Replies are listed 'Best First'.
Re^3: Eugh, regex :(
by Utilitarian (Vicar) on Mar 25, 2009 at 10:09 UTC
    Quick and dirty one line example. you need to escape the [] in the substitution.
    perl -e '$post_message="[URL=http://img207.imageshack.us/my.php?image= +dsc03598vt7.jpg][IMG]http://img207.imageshack.us/img207/2964/dsc03598 +vt7.jpg[/IMG][/URL]"; $post_message =~ s|^.*?\Q[img]\E([\?\%\:\/a-zA- +Z0-9_\-\.]+)\Q[/img]\E.*$|\[img\]$1\[/img\]|sig;print "$post_message\ +n";'
Re^3: Eugh, regex :(
by Marshall (Canon) on Mar 25, 2009 at 15:17 UTC
    My regex was a bit different. find [URL..blah] or [/URL..blah] and delete them with substitution.
    #!/usr/bin/perl -w use strict; my $example = '[URL=http://img207.imageshack.us/my.php?image=dsc03598v +t7.jpg][IMG]http://img207.imageshack.us/img207/2964/dsc03598vt7.jpg[/ +IMG][/URL]'; print "$example \n"; $example =~ s/\[\/*URL.*?\]//g; print $example; #prints [URL=http://img207.imageshack.us/my.php?image=dsc03598vt7.jpg][IMG]htt +p://img207.imageshack.us/img207/2964/dsc03598vt7.jpg[/IMG][/URL] [IMG]http://img207.imageshack.us/img207/2964/dsc03598vt7.jpg[/IMG]
    Update:I recommend Jeffrey Friedl's "Mastering Regular Expressions". This a "classic". But I figure this like nuclear weapons! The vast majority of regex problems can be solved by shooting the problem 1x or 2x or maybe even 3x with simplex regex'es in a sequence. Also I've found that the performance can be just as fast as a single complex regex (and sometimes faster)!