Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Regex: Matching last of repeated character to end of string

by Perl Mouse (Chaplain)
on Nov 04, 2005 at 12:53 UTC ( #505690=note: print w/ replies, xml ) Need Help??


in reply to Regex: Matching last of repeated character to end of string

Greediness or non-greediness is only one rule that Perl applies, and it's not the most important rule. The most important rules are (in that order):

  1. Find a match.
  2. Of all the possible matches, find one that starts the left most in the query string.
  3. If a regex can make choices (alternation, repetition) in multiple places (for instance two alternations, two repetitions, or an alternation and a repetition), choices on the left are more significant than choices on the right. That is, it will try all possibilities of the choice on the right before trying the second alternative of the choice on the left.
  4. In alternation, choices on the left are tried before choices on the right.
  5. In repetion, greedy choices are preferred over non-greedy once - except when there is a ? modifier, then non-greedy choices are preferred over greedy ones.
Your regex could match in two places: starting from the first slash, and starting from the second (last) slash. Starting at the first slash obeys rule 2. Starting at the second slash obeys rule 5. Rule 2 wins.
Perl --((8:>*


Comment on Re: Regex: Matching last of repeated character to end of string
Re^2: Regex: Matching last of repeated character to end of string
by ambrus (Abbot) on Nov 04, 2005 at 22:17 UTC

    While it doesn't really matter here, rule 4 has the same importance as rule 5.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-04-20 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (486 votes), past polls