while( /foo(.*?)(tar)?bar/g ) { next unless $2; ... }
There might be a way to do something very tricky with zero-width assertions, but I find going the simple route is often much easier to maintain.
Updated the above to be simpler and added the following. The above returns "XfooY" for "fooXfooYtarbar" when just "Y" was wanted. So a simple state machine:
- tye (but my friends call me "Tye")my( $state, $prev, @match )= ( "no foo" ); foreach my $tok ( split /(foo|tarbar|bar)/, $string ) { if( "foo" eq $tok ) { $state= "foo"; next; } elsif( "tarbar" eq $tok ) { if( "other" eq $state ) { push @match, $prev; # and/or do other stuff here } } elsif( "bar" ne $tok && "foo" eq $state ) { $prev= $tok; $state= "other"; next; } $state= "no foo"; }
In reply to (tye)Re: repeating patterns 2
by tye
in thread repeating patterns 2
by iradik
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |