regex - What does the following regular expression do? -
foreach(@first) { $first[$r] =~s/<.*>(.*)<.*>/$1/; $first[$r]=$1; $r++; }
what regular expression on line number 3?
the subtitution s/<.*>(.*)<.*>/$1/
looks idiotic attempt remove surrounding html tags string. example, given input
"<p>foo bar <em>baz</em> qux</p>"
we output " qux"
:
<.*>
matches less-than sign, many characters possible, , greater-than sign.(.*)
matches many characters possible , remembers match$1
.<.*>
matches less-than sign, many characters possible, , greater-than sign.
then, whole match replaced contents of capture group 1.
however, code looks written isn't experienced programmer, , doesn't know perl anyway. assuming $r = 0
before loop, equivalent to:
for (@first) { /<.*>(.*)<.*>/; $_ = $1; }
or
@first = map { /<.*>(.*)<.*>/; $1 } @first;
Comments
Post a Comment