Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: pattern matching

by Athanasius (Chancellor)
on Oct 12, 2012 at 16:09 UTC ( #998735=note: print w/replies, xml ) Need Help??


in reply to Re: pattern matching
in thread pattern matching

How about this?

#! perl use strict; use warnings; use YAPE::Regex::Explain; my $re = qr/(.)+/; print YAPE::Regex::Explain->new($re)->explain;

Output:

The regular expression: (?-imsx:(.)+) 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)): ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- )+ 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) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Not strictly documentation, but certainly confirmation that “Captures with quantifiers only return the last match.”

Update: Here is the official documentation:

/(\d)(\d)/ # Match two digits, capturing them into $1 and $2 /(\d+)/ # Match one or more digits, capturing them all into $1 /(\d)+/ # Match a digit one or more times, capturing the last into + $1

Note the difference between the second and third patterns. The second form is usually what you want. The third form does not create multiple variables for multiple digits. Parentheses are numbered when the pattern is compiled, not when it is matched.

The Camel Book, 4th edition, pages 221–222.

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^3: pattern matching
by perltux (Scribe) on Oct 12, 2012 at 16:12 UTC
    Thanks, I believe you are right, but how do I get then all matches as individual elements into the array?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998735]
help
Chatterbox?
[Corion]: Yay. FF now has u2f built-in (behind an about:config setting), so using an USB token to sign in actually works :)
[Corion]: So, it's not all bad with the new FF :)
[holli]: 2factor is a bit too much for me. i just use passwords like jhbn8789q27312laks dmaw98^
[moritz]: isn't there also some about:config setting to enable the old plugin API?
[Discipulus]: indeed, only the logo is nicer
[Corion]: moritz: Hmm, I doubt so, as I think they moved away from XUL and other UI stuff, which was used by most plugins I used
[Corion]: holli: I'm trying it out with Github and so far I don't dislike it. It's basically like an extra key I press to log in, which is an OK flow for Github
[Corion]: I haven't tried out recovery in case of a broken USB key though (even though I have three such U2F tokens lying around here)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-11-20 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (293 votes). Check out past polls.

    Notices?