Re: Do nothing? or Do something important in a very obscure way?
by TheDamian (Vicar) on Aug 23, 2002 at 13:06 UTC
|
Can I just point out that a colon doesn't mean "don't tracktrack past this point". It means "don't backtrack *into* the preceding repetition (to try a different number
of matches), but skip back over it instead".
And to answer the original question, x??: and x*?: are exactly the same as x<0> (except that x??: returns a scalar-ish match object, rather than the list-ish one returned by x*?: and x<0>). | [reply] [d/l] [select] |
|
|
More precisely, it means to means to ignore any decision points within the previous atom. Those decision points may or may not have been generated by a quantifier on the entire atom. For instance, in [a|b]: the colon disables trying the second alternative if the first alternative succeeds.
| [reply] [d/l] |
|
|
I'm not quite sure who said "don't tracktrack past this point" or even "don't backtrack past this point"? I know it wasn't me:)
That leaves me with the same dilema though. What does x<0> mean?
Does asking to match something exactly zero times mean:
- Don't do anything?
- Or: Don't accept an x here.
- Or something else which I haven't underdstood?
I'm only just getting to grips with Perl5 regexs, and have only used the 'simple' ones so far, but the idea of instructing the regex engine to match something zero times seems nonsensical to me at this stage. Unless it means "Fail if there is an x here", but that would be the same as [^x] or -[x] (or is that -<x>) in the new money?
What's this about a "crooked mitre"? I'm good at woodwork!
| [reply] [d/l] [select] |
|
|
What does x<0> mean?
It means "Don't do anything", or rather: "ignore that x". Just as x{0} does in Perl 5.
Don't accept an x here?
No. That's <!before x> in Perl 6.
Or (?!x) in Perl 5.
Which is not the same as "Accept something that isn't an x". That would be <-[x]> in Perl 6 and [^x] in Perl 5.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
Re: Do nothing? or Do something important in a very obscure way?
by theorbtwo (Prior) on Aug 23, 2002 at 08:14 UTC
|
Well, let's look at it from the regex engine's point of view, executing 'xfoo' =~ m/x??:foo/;. First, it tries to match x zero times (since x? can match zero times, and x?? tells it to be non-greedy about it, it starts with zero, and backtraces up from there). That succeeds. Then, it tries to match foo, fails, and backtraces. But ohno, it can't backtrace. But what it can do is try matching the whole regex a little later (I'm not sure; does this count as backtracing past the ":"?)
Confession: It does an Immortal Body good.
| [reply] [d/l] |
|
|
I'm genuinely not sure if you answered my dilemma or not:)
Does "it tries to match x zero times" mean, it just moves on to the next part of the regex and doesn't bother looking at the string under inspection, effectively completely ignoring the presence of 'x??' or 'x*?'? Or does it look to make sure that there isn't an 'x' in the string between the current point and the next significant point?
What's this about a "crooked mitre"? I'm good at woodwork!
| [reply] |
|
|
The RE engine is a recursive search for ways to try to match the pattern to the string.
'x??' means that it notes that it has a decision, and first tries to match no x's. If it fails then it will backtrack, reconsider that decision, and try to match one x then go forward. If that fails, then you fail.
'x*?' is similar. It notes that it has a decision, and first tries to match no x's. If it fails then it will backtrack, try to match one x, and then try to make the same decision over again. (That is the difference between the * and the ?. The ? will try to match at most once then proceed. The * returns to trying to match the preceeding group, and therefore can match there an unlimited number of times.)
If that didn't make sense then pick up Mastering Regular Expressions by Friedl. He explains it better.
| [reply] |
|
|
|
|
| [reply] |
Re: Do nothing? or Do something important in a very obscure way?
by blakem (Monsignor) on Aug 23, 2002 at 10:49 UTC
|
/x?/ means match x zero or one times, but prefer 1... i.e. Match it if you can
/x??/ means match x zero or one times, but prefer 0... i.e. Match it if you have to
For instance:
% perl -le '"wxy" =~ /(wx?)/; print $1'
1:wx
% perl -le '"wxy" =~ /(wx??)/; print $1'
1:w
% perl -le '"wxy" =~ /(wx?y)/; print $1'
1:wxy
% perl -le '"wxy" =~ /(wx??y)/; print $1'
1:wxy
The notation is a little confusing because the two question marks in a row mean different things. /x?/ is really just syntatic sugar for /x{0,1}/. The second question mark turns on min matching. When you look at it this way, the difference between /x{0,1}/ and /x{0,1}?/ should be more clear.
-Blake
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Ah, I see now.... /x??:/ will behave exactly like /x{0}/ In english /x??:/ would read something like this:
Match 'x' zero or one times, prefer 0 over 1, lock in the first value that allows this atom to match (i.e. don't backtrack).
Since the 0 will always cause the atom to match, the engine will never be allowed to try the 1.
I know thats not the best description.... hope it helps.
-Blake
| [reply] [d/l] [select] |
Re: Do nothing? or Do something important in a very obscure way?
by Anonymous Monk on Aug 23, 2002 at 08:03 UTC
|
That doesn't make sense, neither one is a way of saying match exactly
zero repetitions of x.
| [reply] |
|
|
nevermind, I missed the colons!
| [reply] |
Re: Do nothing? or Do something important in a very obscure way?
by John M. Dlugosz (Monsignor) on Aug 30, 2002 at 21:19 UTC
|
| [reply] |