"Pandey Rajeev-A19514" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi ,
Hello > > I need some help me to extract a pattern. The delimiters is a pair of "abcd" and "efgh". Can some one help me with an efficient use of Greedy and non greedy matches, look ahead and lookbehind features. > > I want the smallest match involving the two delimiters. I want all the matches. i.e. fisrt match , last match and all ther other matches in between. > Why do this with a regex? Just because it's written in perl doesn't mean you _have_ to program in regexes. How about the following solution, finding the delimiters and printing all the combos? #/perl -w my $string = " abcd 1 edfg 2 abcd edfg 3 4 5 abcd 6 7 edfg 8 9"; my $start_delim = 'abcd'; my $end_delim = 'edfg'; my @start_indices; my @end_indices; my $current_index = 0; while ( ($current_index = index($string, $start_delim, $current_index + 1)) ne -1) { push @start_indices , $current_index}; $current_index = 0; while ( ($current_index = index($string, $end_delim, $current_index + 1)) ne -1) { push @end_indices , $current_index}; foreach my $start (@start_indices) { foreach my $end (@end_indices) { next if ($start > $end); print "$start, $end\n"; } } > > Here is what I tried to extract the first match and the results(a bad one) I got. > > $abc = <<END; > aaaaaaaaaa > abcd aaaaa > abcd bbbbb > AAAAAAAAAAAAAAA > BBBBBBBBBBBBBBB > efgh ccccc > CCCCCCCCCCCCCCC > efgh ddddd > DDDDDDDDDDDDDDD > EEEEEEEEEEEEEEE > FFFFFFFFFFFFFFF > abcd eeeee > abcd fffff > GGGGGGGGGGGGGGG > HHHHHHHHHHHHHHH > IIIIIIIIIIIIIII > efgh ggggg > efgh hhhhh > JJJJJJJJJJJJJJJ > KKKKKKKKKKKKKKK > END > > $match = 'abcd(?:(?!abcd).)*efgh.*?\n'; > ($found) = ($abc =~ /($match)(?!$match)/sxi); > print "*************\nFOUND FIRST MATCH\n$found\n"; > > The OUTPUT IS : > ************* > FOUND FIRST MATCH > abcd bbbbb > AAAAAAAAAAAAAAA > BBBBBBBBBBBBBBB > efgh ccccc > CCCCCCCCCCCCCCC > efgh ddddd > > > WHEREAS I wanted it to be > ************* > FOUND FIRST MATCH > abcd bbbbb > AAAAAAAAAAAAAAA > BBBBBBBBBBBBBBB > efgh ccccc > > > Any suggestion is welcome. > > Thanks in advance. > Rajeev > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]