The stupid question is the question not asked | |
PerlMonks |
Printing out matches for two regular expressionsby Maire (Scribe) |
on Oct 22, 2017 at 08:29 UTC ( [id://1201828]=perlquestion: print w/replies, xml ) | Need Help?? |
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:
So, for example, from the above file, I would expect the output to be:
At the moment, I am using the following script:
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
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."
Any help would be greatly appreciated!
Back to
Seekers of Perl Wisdom
|
|