in reply to Can you do "conjunctive" (overlapping) conditions in a single regexp?
Be warned though that this will backtrack and try to match again. For instance if you want to not match more than 4 ,'s, this will match 4 out of 6 commas, then will match 2 commas next time. So instead of preserving 6 commas you'll eat them up and get a blank field.@x = split /(?{ # This populates $^R pos() })some pattern(??{ # This returns an impossible pattern for long matches (pos()-$^R < 5) ? "" : "no\\bmatch" })/, $string;
If this is not what you want then you have to do a lot more work. You have to make sure that it fails on all of the backtracks as well. This means that when you fail you need to remember the failure and fail every time you see that position. Like this:
That is ugly! But it should work.# Be sure that these start clean! my %bad_r; my %bad_pos; @x = split /(?{ # This populates $^R pos() })some pattern(??{ # This returns an impossible pattern for long matches if (pos()-$^R < 3 and not $bad_r{$^R} and not $bad_pos{pos()} ) { ""; } else { $bad_r{$^R}++; $bad_pos{ pos() }++; "no\\bmatch" } })/, $string;
Update: Well it should if I had not missed the closing paren on the pos(). Thanks to ikegami for catching that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can you do "conjunctive" (overlapping) conditions in a single regexp?
by ikegami (Patriarch) on Dec 18, 2008 at 03:08 UTC | |
by tilly (Archbishop) on Dec 18, 2008 at 05:28 UTC | |
|
Re^2: Can you do "conjunctive" (overlapping) conditions in a single regexp?
by puterboy (Scribe) on Dec 18, 2008 at 03:46 UTC | |
by tilly (Archbishop) on Dec 18, 2008 at 04:55 UTC | |
by puterboy (Scribe) on Dec 18, 2008 at 05:52 UTC | |
by tilly (Archbishop) on Dec 18, 2008 at 19:37 UTC | |
by ikegami (Patriarch) on Dec 18, 2008 at 19:41 UTC |