Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: a regex pattern how to understand

by Thai Heng (Beadle)
on Jul 08, 2015 at 22:54 UTC ( [id://1133837]=note: print w/replies, xml ) Need Help??


in reply to Re: a regex pattern how to understand
in thread a regex pattern how to understand

The space matched in the former [[:alpha:] ], and can't matched in the last \s+Age:。So I think the text can't matched the reg pattern. Because there is only one space between name and age.

Replies are listed 'Best First'.
Re^3: a regex pattern how to understand
by Anonymous Monk on Jul 08, 2015 at 23:11 UTC
    The space matched in the former [[:alpha:] ], and can't matched in the last \s+Age:。So I think the text can't matched the reg pattern. Because there is only one space between name and age.

    Hmm, if perl matches it, why do you think that is?

    If you turn on use re 'debug' ; what do you see?

    I see

    149 <Princ> <e Age: 53> | 21: CLOSE1(23) 149 <Princ> <e Age: 53> | 23: PLUS(25) SPACE can match 0 times out of 2 +147483647... failed... ANYOF[ A-Za-z][{unicode}+utf8::XPo +sixAlpha 00AA 00B5 00BA 00C0-00D6 00D8-00F6 00F8- 02C1] can match 1 times out of 1... 150 <rince> < Age: 53%n> | 21: CLOSE1(23) 150 <rince> < Age: 53%n> | 23: PLUS(25) SPACE can match 1 times out of 2 +147483647... 151 <ince > <Age: 53%nO> | 25: EXACT <Age:>(27) 155 < Age:> < 53%nOccup> | 27: PLUS(29) SPACE can match 1 times out of + 2147483647...

    +? means match the least amount possible, the least amount doesn't include space, because the next pattern wants space

Re^3: a regex pattern how to understand
by Anonymous Monk on Jul 08, 2015 at 23:26 UTC

    As the other anon already said, the +? modifier makes the expression non-greedy, meaning it doesn't consume all possible characters. Even if the ? is removed, the regular expression still works due to Backtracking:

    For a regular expression to match, the entire regular expression must match, not just part of it. So if the beginning of a pattern containing a quantifier succeeds in a way that causes later parts in the pattern to fail, the matching engine backs up and recalculates the beginning part--that's why it's called backtracking.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found