Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Regex to match multiple words in an email subject

by brainfold (Initiate)
on Jan 07, 2011 at 04:41 UTC ( #881006=perlquestion: print w/ replies, xml ) Need Help??
brainfold has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I am writing a script for Resource Tracker to look for specific key words in a email subject and send them to different queue name

.

This script below works for single search word, but i wanted to use multiple key word

Custom Action Preparation code

my $match = (".*SSL.*"|".*SSL Certificate.*"|".*VASCO.*"); my $t_subject = $self->TicketObj->Subject; if ( $t_subject !~ /$match/i ) { return 0; } else { return 1; }

Custom action clean up code

my $newqueue = "Key Management"; my $T_Obj = $self->TicketObj; $RT::Logger->info("Auto assign ticket #". $T_Obj->id ." to queue #". $ +newqueue ); my ($status, $msg) = $T_Obj->SetQueue($newqueue); unless ($status) { $RT::Logger->warning("unable to set new queue: $msg"); return undef; } return 1;

Issue

Here the issue is with the $match, the script works when i have only one key word. Say if i have more than one key word (in this instance SSL, SSL Certificates, Vasco)then the script is not working.

Is there any alternate way i can follow to overcome this

Can any one please help me out with this??, Please

Thanks

Comment on Regex to match multiple words in an email subject
Select or Download Code
Re: Regex to match multiple words in an email subject
by Anonymous Monk on Jan 07, 2011 at 04:51 UTC
    A simple $t_subject =~ /foo/ will tell you if "foo" appears in the string. If "foo" might have special characters you want to match literally, /\Qfoo\E/ or just /\Qfoo/ or using quotemeta is necessary.

    For multiple matches, it depends on the logic. If you want to match on any one, you could simply do

    if (grep { $t_subject =~ $_ } qr/foo/, qr/bar/, qr/baz/) {

    or

    if (grep { $t_subject =~ $_ } map quotemeta, 'foo', 'bar', 'baz') {

    On Perl 5.10 and later, you could even do

    if ($t_subject ~~ [qr/foo/, qr/bar/, qr/baz/]) {

Re: Regex to match multiple words in an email subject
by AnomalousMonk (Abbot) on Jan 07, 2011 at 05:19 UTC

    One problem with your action preparation code as given in the OP is that the  | (vertical bar) operator has nothing with regular expressions. Rather, it is a bitwise-or operator that is used on multiple strings.

    >perl -wMstrict -le "my $match = (\".*SSL.*\"|\".*SSL Certificate.*\"|\".*VASCO.*\"); print qq{'$match'}; " '.*WS_oooztificate.*'
Re: Regex to match multiple words in an email subject
by philipbailey (Chaplain) on Jan 07, 2011 at 05:33 UTC
    #!/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
      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: perlquestion [id://881006]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-09-19 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls