Dear All,
My program should match an element in a text, and print the sentence with a title preceding it (all sentences have titles). However in the loop I created, the matching of the element I'm looking for is completely ignored and it prints all the lines (it idetifies the title only). Where have I gone wrong??
Thanks,
Rami
#!/usr/local/bin/perl use strict; use warnings; ########################################################### #In this program there is a text always ontaining a title #followed by + # #a sentence.I wish to check if there is a regexp B in the #actual sent +ence. # #If so, I wish to print the title of this sentence and the #sentence i +tself # #If the element B does not appear in the sentence, I want #both the se +ntence # #and its title to be #ignored. + # ###################################################################### +####### my $text = "A This is a title of the first sentence some text here containing the element B I need and more text here A This is a title of the second sentence C some text here but no element I need so it should be ignored A This is a title for the third sentence last sentence with the element B that I'm looking for"; my $expa = "A"; #title element which appears before every #sentence my $expb = "B"; #the regexp I'm loooking for my $line; # the line my $s; while($line = $text){ if($line =~m/$expa/){ #if A is found $s = $line; #then, read the following line $line = $text; if($line =~m/$expb/){ #if B is found print "$s\n\t$line\n\n\n"; #print both lines } } }
20041002 Janitored by Corion: Added code tags and formatting
In reply to RegExp Loop by ramigi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |