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.
In reply to Re: regex: something...(!something)...something
by GrandFather
in thread regex: something...(!something)...something
by aaaone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |