in reply to regex: something...(!something)...something

For markup use the appropriate parsing module. That will save you a pile of time and substantive questions won't get clouded by the "but you should be using a module" answer - you've preempted that answer and move on to real issues.

So, lets look at the real issue without the distraction of HTML, XML, ... . Consider:

use strict; use warnings; my $match1 = 'something '; my $fill = 'xxxxxx '; my $match2 = 'somethingelse '; my $nomatch = 'nomatch '; my @targets = ( "$nomatch$fill$match1$match2$nomatch", "$nomatch$fill$match1$fill$match2$nomatch", "$nomatch$fill$match1$fill$nomatch$fill$match2$nomatch", ); for my $test (@targets) { if ($test =~ /$match1 ((?:(?!$nomatch) .)*) $match2/x) { print "Matched >$1< for: $test\n"; } else { print "Failure for $test\n"; } }

Prints:

Matched > < for nomatch xxxxxx something somethingelse nomatch Matched > xxxxxx < for nomatch xxxxxx something xxxxxx somethingelse n +omatch Failure for nomatch xxxxxx something xxxxxx nomatch xxxxxx somethingel +se nomatch

The trick is the (?:(?!$nomatch) .)* which will only match a character if it is not the start of the nomatch criteria.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: regex: something...(!something)...something
by aaaone (Initiate) on Jul 18, 2008 at 12:53 UTC
    I begin to understand the idea of trick) thank you.
    In my case the regex must be more complex, I'll soon post the solution in my first post