Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regular expression in Perl/Curl script

Tags:

regex

perl

I'm not all that sure how this works/what it means...

my ($value) = ($out =~ /currentvalue[^>]*>([^<]+)/);

So basically, thats part of a CURL/PERL script, it goes onto www.example.com, and finds
<span id="currentvalue"> GETS THIS VALUE </span>
in the pages html.

What exactly does the [^>]*>([^<]+)/) part of the script do? Does it define that its looking for span id=".." ?

Where can I learn more about the [^>]*>([^<]+)/) functions?

like image 605
Ilan Kleiman Avatar asked Jan 28 '26 02:01

Ilan Kleiman


1 Answers

/.../ aka m/.../ is a the match operator. It checks if its operand (on the LHS of =~) matches the regular expression within the literal. Operators are documented in perlop. (Go down to "m/PATTERN/".) Regular expressions are documented in perlre.

As for the regular expression used here,

$ perl -MYAPE::Regex::Explain \
   -e'print YAPE::Regex::Explain->new($ARGV[0])->explain' \
      'currentvalue[^>]*>([^<]+)'
The regular expression:

(?-imsx:currentvalue[^>]*>([^<]+))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  currentvalue             'currentvalue'
----------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  >                        '>'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^<]+                    any character except: '<' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
like image 73
ikegami Avatar answered Jan 29 '26 15:01

ikegami