Hi , being new to Perl I'm struggling for a simple explaination on how something like the following code would work.
$text = "The dog is black cat is white and the fox does not like the c +ow or pig"; if ($text =~ /(dog|cow|pig)/) { print "match found" }
Does the match quit once it finds the first occurence of one of the alternatives ? e.g. in this case it finds a match on dog so exits
then If I add /g
/(dog|cat|fox|cow|pig)/g
Will that then try and match each alternative in the string and then return ?
What happens if I wanted to build a regular expression but have it continue to search the string for the other alternatives even if the first is matched e.g. I may wish to split the above string up based on the alternatives provided , say put a new line in just before where a match is found. So I end up with the following output
The dog is black cat is white and the fox does not like the cow or pig
Can I do that using the alternation (dog|cat|fox|cow|pig) ?

In reply to Understanding alternation by Anonymous Monk

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.