Below is a simplified version of the code that I'm trying to get to work:
my $str = "start b2 end start b2 b2 end start b2 end";
if ($str =~ /start(?!.*start.*)(.*?b2.*?b2.*?end)/) {
print "Regexp matched!";
}
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 look-ahead and look-behind 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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.