Here is my text:

>>>cat temp.txt my dog is (red1) my cat is (blue1) my dog is (red2) my cat is (blue2) my dog is (red3) my cat is (blue3) my dog is (red4) my cat is (blue4) my dog is (red5) my cat is (blue5) my dog is (red6) my cat is (blue6)

Here is my code:

#!/usr/bin/perl $mystring = join("", <>); #print $&; print "-----------mystring--------------------------\n"; print $mystring; print "------------variables----------------------------\n"; if ($mystring =~ m/my.*?\((\S*?)\)/g) { #if ($mystring =~ s/my.*?\((\S*?)\)/xxxxxxx/g) { print "$1 \n"; print "-----------mystring again--------------------------\n"; print $mystring; print "-----------------------------------------\n"; }

Here is the output:

>>>>cat temp.txt | perl temp.pl -----------mystring-------------------------- my dog is (red1) my cat is (blue1) my dog is (red2) my cat is (blue2) my dog is (red3) my cat is (blue3) my dog is (red4) my cat is (blue4) my dog is (red5) my cat is (blue5) my dog is (red6) my cat is (blue6) ------------variables---------------------------- red1 -----------mystring again-------------------------- my dog is (red1) my cat is (blue1) my dog is (red2) my cat is (blue2) my dog is (red3) my cat is (blue3) my dog is (red4) my cat is (blue4) my dog is (red5) my cat is (blue5) my dog is (red6) my cat is (blue6) -----------------------------------------

What I can't figure out is how to print each one as it is matched. If I do a global replace, it replaces all of them (commented out that line). So, it is able to match each of them. I'm sure I'm missing an easy perl fuction or special variable. I've looked through Perl bookshelf and also in frequent questions of perlmonks.com


Basically I need to itterate over the matches. I tried a while statement and that doesn't work either.

Matching each line separate with a while <> won't work either. This is just a sample, but the pattern I'm really matching spans multiple lines. The join is necessary for that.


In reply to variables in global match in regex by BrassMonkey

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.