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
In reply to Re: regexp problem
by tachyon
in thread regexp problem
by Ignatius Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |