http://qs321.pair.com?node_id=1194580


in reply to Re^2: Grabbing numbers from a URL
in thread Grabbing numbers from a URL

According to Quote and Quote Like Operators, the q operator does not interpolate the string. So, this code
$url = q|$itemID|;
results in $url contains the literal string $itemID, not the contents of $itemID. To test this out, run the following code
#!/usr/bin/env perl use strict; use warnings; my $itemID = 'something-12345.html'; my $url = q|$itemID|; $url =~ /(\d{4,5})\.htm/i; print "Item ID: $itemID\n"; print " URL: $url\n"; my $num = $1; print " NUM: $1\n"; exit;
You should get an error, since there is no number found in the URL. If you change the code to use the qq operator, then the string is interpolated, the match succeeds and you get the number in the $1 capture group variable.
#!/usr/bin/env perl use strict; use warnings; my $itemID = 'something-12345.html'; my $url = qq|$itemID|; $url =~ /(\d{4,5})\.htm/i; print "Item ID: $itemID\n"; print " URL: $url\n"; my $num = $1; print " NUM: $1\n"; exit;
If you want a description of regular expression in plain english, then you can use the YAPE::Regex::Explain module. Running this one-liner on your regex
perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new("(\d{4}) ++\.htm")->explain();'
The result is
The regular expression: (?-imsx:(d{4})+.htm) 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): ---------------------------------------------------------------------- ( group and capture to \1 (1 or more times (matching the most amount possible)): ---------------------------------------------------------------------- d{4} 'd' (4 times) ---------------------------------------------------------------------- )+ end of \1 (NOTE: because you are using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1) ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- htm 'htm' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
You can compare this to one of the modified regex patterns that I gave you. For example,
perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new("(\d{4,5 +})\.htm")->explain();'
The regular expression: (?-imsx:(d{4,5}).htm) 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): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- d{4,5} 'd' (between 4 and 5 times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- htm 'htm' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------