swackerl has asked for the wisdom of the Perl Monks concerning the following question:
An explanation of what I'm trying to match is a substring of $str where it is made up of an 'start' marker, followed by two 'b2' markers, followed by an 'end' marker (with any text, not just whitespace, interspersed between the markers that I've mentioned). Think of 'start' as marking the start and 'end' marking the end of the possible string. I don't want the expression to overlap between two strings such as in: "start b2 end start b2 b2 end" where the matched string contains an additional 'start' marker. I've tried doing this with lookahead and lookbehind assertions. The problem with a lookahead assertion is that it will search to the end of the string, so if there is any 'start' after the string that I want to match, no matches will be found. The problem with a lookbehind assertion is that it does not allow for variable length strings. Can anyone help me solve this difficult problem?my $str = "start b2 end start b2 b2 end start b2 end"; if ($str =~ /start(?!.*start.*)(.*?b2.*?b2.*?end)/) { print "Regexp matched!"; }
Originally posted as a Categorized Question.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Keeping lookahead assertions from looking until the end of the string?
by swackerl (Initiate) on Sep 04, 2002 at 23:18 UTC |