Maire has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am trying to get a very basic script to print out the matches from two regular expressions at once. Specifically, I am trying to print out all of the numbers (digits) and all of the words in between a "#" and the word "fin" in .txt files which take the following format:
The 2 cats and the dog. The 8 cats and the 6 dogs. The 3 pigs and the 2 sheep. #story fin #cats and dogs fin #sheep fin
So, for example, from the above file, I would expect the output to be:
2 8 6 3 2 story cats and dogs sheep
At the moment, I am using the following script:
open(FILE, 'C:\Users\li\perl\animals.txt'); $/ = " "; while (<FILE>) { if (m/((\d)+?)|((?<=#)(.*?)(?= fin))/g) { print "$1\n"; } }
However, while this returns the numbers, it does not return the desired words. I believe that my mistake is using the | operator, which I think is telling the script to finish becuase it has found the first part of the regex and doesn't need to continue for the rest?
A google search suggested that lookaheads could be used in a way that mirrors an "and" operator: (?=.*word1)(?=.*word2)(?=.*word3) (http://www.ocpsoft.org/tutorials/regular-expressions/and-in-regex/) However, the following regex, created using the lookaheads suggested above, returns no results for me
open(FILE, 'C:\Users\li\perl\animals.txt'); $/ = " "; while (<FILE>) { if (m/(?=.*((\d)+?))(?=.*((?<=#)(.*?)(?= fin)))/g) { print "$1\n"; } }
I also read about using Smart Match How do I efficiently match many regular expressions at once?. However, when I run the following script, the only thing that appears is a notification that "Smartmatch is experimental at C:\Users\li\perl\animalscript2.pl line 3."
open(FILE, 'C:\Users\li\perl\animals.txt'); my @patterns = ( qr/((\d)+)/, qr/((?<=#)(.*?)(?= fin))/); if( $string ~~ @patterns ) { print "$1\n"; };
Any help would be greatly appreciated!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing out matches for two regular expressions
by choroba (Cardinal) on Oct 22, 2017 at 09:08 UTC | |
by Maire (Scribe) on Oct 22, 2017 at 10:07 UTC | |
by AnomalousMonk (Archbishop) on Oct 22, 2017 at 14:50 UTC | |
by Maire (Scribe) on Oct 23, 2017 at 06:40 UTC |