Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Matching patterns with or

by AnomalousMonk (Archbishop)
on Jun 19, 2018 at 18:32 UTC ( [id://1216961]=note: print w/replies, xml ) Need Help??


in reply to Matching patterns with or

Another way:

c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'The <A} test {B> sentence <C} foo {42} bar {D> baz' +; ;; while (m{ \G .*? ({\d+} | {\w+> | <\w+}) }xmsg) { printf qq{'$1' }; } " '<A}' '{B>' '<C}' '{42}' '{D>'

Update 1: Changed example code slightly so that the test sentence includes all sub-patterns.

Update 2: On second thought, the  \G .*? part of this regex contributes nothing in this particular application; leaving it out does not change the behavior of the match. As Eily wrote, the  /(A|B|C)/g pattern is all you need.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-18 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found