Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You could also consider a more difficult example where you are trying to match lines a data that fall into different "types", so you have different regexs to catch them, even though you are trying to collect the same data regardless of the form (clear as mud so far, right?).

Let's just say that you end up with three different regexs. You deside to write three different ones because it would have made the single combined regex a god-awful mess. Good. Now, if you use Hofmator's suggestion, you'll get something like this:

if ($string =~ /re1/ || $string =~ /re2/ || $string =~ /re3/)
where $1 will be populated with what you are looking for, but you still may not know which of re1, 2 or 3 matched. In this case I would use a bigger if. If you want to know which regex matched, it is presumably because you want to do different things with the results. So put them all in if..else's:
if ( $string =~ /re1/ ) { #do thing 1 } elsif ( $string =~ /re2/ ) { #do thing 2 } elsif ( $string =~ /re3/ ) { #do thing 3 }
It is bulkier than other suggestions, but I suspect it is a more generally useful answer to your question.

Scott


In reply to Re: Returning _which_ pattern matched...? by scain
in thread Returning _which_ pattern matched...? by Boudicca

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-04-01 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?