List Info

Thread: Greedy behavior on conditionals




Greedy behavior on conditionals
user name
2006-01-12 06:24:28
Hi all!

I have a regular expression like:

m{
    blah1
    (blah2)?
    (blah3)?
}gisx;

I want this to match 'blah2' and 'blah3' if there is such a
match, and
'blah3' to match only when 'blah2' matches. However Perl
RegEx engine
simply quits after matching 'blah1' (non-greedy). What I
attempted was:

m{
    blah1
    (blah2)?
    (blah3)?
    (?(?{!$1})(?!))  # [1]
    (?(?{!$2})(?!))  # [2]
}gisx;

[1] & [2] forces Perl to backtrack if the optional
phrases didnot match
yet, and simply insert 'nothing' if there is a match. There
is one
problem however; when the input text is large and there is
no 'blah2'
or 'blah3' at all, the RegEx will backtrack uncountable
times!

How can I go about changing the behavior of RegEx engine for
it to
attempt a match first and then bail out in case of no-match
for
conditional phrases? Is there a special switch for this? I
also believe
it could be achieved with some smart regex, but couldnot
devise one
yet... Maybe I am missing something obvious!

Thank you all!

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )