in reply to regexp problem
First let me agree with the above advice that usnig a specialist HTML parser is the best option. This said here is the solution you want:
$_ = '<a href="foo">/path/sub/dir</a>'; $d = '/path'; $d = quotemeta $d; print $1 if /"\s*>.*?$d(.*?)<\/A>/i; # prints /sub/dir # here it is with the regex expanded $_ = '<a href="foo">/path/sub/dir</a>'; $d = '/path'; $d = quotemeta $d; print $1 if m # match regex / # opening delim " # literal " \s* # plus 0 or more spaces > # end of <a href tag .*? # some leading stuff (minimum) $d # our path (.*?) # out subdirs captured into $1 <\/A> # the closing tag /ix; # /i => case insensitive for </a> tag # /x => allow comments
You need the quotemeta to make $d safe to interpolate into the regex, otherwise the / used as a path delimiter will be misinterpreted as the closing regex delimiter ie after interpolating $d the regex would be /"\s*>.*?/path(.*?)<\/A>/i
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: regexp problem
by Ignatius Monk (Novice) on Jul 09, 2001 at 11:22 UTC |