Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl: the Markov chain saw
 
PerlMonks  

Re: why is regex not matching final character?

by AnomalousMonk (Prior)
on May 07, 2012 at 11:31 UTC ( #969247=note: print w/ replies, xml ) Need Help??


in reply to why is regex not matching final character?

I thought \w would gobble up the whole table name first and [^\s] would stop the gobbling at the first space.

And that's just what happens. In, e.g., 'users', the  (\w*) gobbles (and captures) 'user', and the  [^\s] gobbles (and swallows) 's'. But all that's just what moritz just said.

Another way of looking at the regex (or any pre-Perl 5.7 regex) is with YAPE::Regex::Explain.

>perl -wMstrict -le "use YAPE::Regex::Explain; ;; my $rx = qr/^update\ (\w*)[^\s]/; print YAPE::Regex::Explain->new($rx)->explain; " The regular expression: (?-imsx:^update\ (\w*)[^\s]) 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): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- update 'update' ---------------------------------------------------------------------- \ ' ' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- \w* word characters (a-z, A-Z, 0-9, _) (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- [^\s] any character except: whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------


Comment on Re: why is regex not matching final character?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969247]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (24)
As of 2013-06-19 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many continents have you visited?









    Results (658 votes), past polls