Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Regex to match multiple words in an email subject

by philipbailey (Chaplain)
on Jan 07, 2011 at 05:33 UTC ( #881013=note: print w/ replies, xml ) Need Help??


in reply to Regex to match multiple words in an email subject

#!/usr/bin/perl use warnings; use strict; for (<DATA>) { print /(?:SSL|SSL Certificate|VASCO)/ ? "Match\n" : "No match\n"; } __DATA__ SSL SSL Certificate VASCO something else

Output:

Match
Match
Match
No match


Comment on Re: Regex to match multiple words in an email subject
Download Code
Replies are listed 'Best First'.
Re^2: Regex to match multiple words in an email subject
by jwkrahn (Monsignor) on Jan 07, 2011 at 07:22 UTC
    print /(?:SSL|SSL Certificate|VASCO)/ ? "Match\n" : "No match\n";

    Because the pattern is not anchored at either end the string 'SSL Certificate' will never match.

    $ perl -le' my @strings = ( "SSL", "SSL Certificate", "VASCO", "something else", ); for ( @strings ) { print $1 if /(SSL|SSL Certificate|VASCO)/; } ' SSL SSL VASCO

    With no anchors you need to put the longer strings first:

    $ perl -le' my @strings = ( "SSL", "SSL Certificate", "VASCO", "something else", ); for ( @strings ) { print $1 if /(SSL Certificate|SSL|VASCO)/; } ' SSL SSL Certificate VASCO

      That's a good point, but in the OP's use case it's irrelevant. In fact, the regex could be trimmed down to:

      /(SSL|VASCO)/

      Or, to avoid unnecessary capturing:

      /(?:SSL|VASCO)/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2015-07-07 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls