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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -