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!


In reply to Printing out matches for two regular expressions by Maire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.