in reply to Regex look ahead
Well, if you read the comments there, we start matching at foo. We match character by character (unless we get to the beginning of baz). Then, we must match bar.
Going through your string, we start at foo. Good. We check if we're at the beginning of baz - we aren't. Grab a character (' '). Still not at the beginning of baz, so we grab another character (' a'). Still not at the beginning of baz, so we grab another character (' a '). Now we're at the beginning of baz, so we obviously went too far - back off (' a'), and exit the "loop". Next we need to match 'bar' - but 'bar' isn't here. Thus we failed to match anything. (Actually, we'll back off the two characters that we did match in the complex expression, but that will still fail to find 'bar'.)
One rule of thumb is to always check if a match succeeds before using any of the special variables. So try something like this:
Hope that helps,if ( /foo # Match starting at foo (?: # Complex expression: (?!baz) # make sure we're not at the beginning of baz . # accept any character )* # any number of times bar # and ending at bar /x ) { # print stuff out. } else { print "Failed to match against '$_'\n"; }
|
|---|