Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Understanding alternation

by g0n (Priest)
on Feb 03, 2006 at 15:06 UTC ( [id://527673]=note: print w/replies, xml ) Need Help??


in reply to Understanding alternation

'Does the match quit once it finds the first occurence of one of the alternatives?'
Yes.

'Will that then try and match each alternative in the string and then return?'
Sort of. It will keep looking through the string, even after it's found a match, until it doesn't find any more matches. It's a fine distinction, but it doesn't look through the string for the first, then look through again for the second etc, AFAIK. Note that in the context you are using it, it will just return true, not a list of matches.

'Can I do that using the alternation ..'
Yes, like this:

$text = "The dog is black cat is white and the fox does not like the c +ow or pig"; $text =~ s/(dog|cow|cat|pig)/\n$1/g; print $text;

($1 contains the value matched by the pattern, so you are replacing each match with itself, prepended by \n).

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".

Replies are listed 'Best First'.
Re^2: Understanding alternation
by McDarren (Abbot) on Feb 03, 2006 at 15:15 UTC
    I'd throw a couple of \b's in there, so it only matches on word boundaries. eg:
    $text =~ s/\b(dog|cat|fox|cow|pig)\b/\n$1/g;
    Otherwise, you'd match stuff like "cowardly" and "pigheaded" ;)
Re^2: Understanding alternation
by Anonymous Monk on Feb 03, 2006 at 15:38 UTC
    Ok seems fairly straightforward thanks for the help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://527673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-18 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found