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

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;


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


Replies are listed 'Best First'.
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/) {


    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 (Chancellor) 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


    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:


        Or, to avoid unnecessary capturing:


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://881006]
Approved by ikegami
[choroba]: three
[choroba]: I mean I haven't used DBIx::Class nor M:S, only Moo
[1nickt]: Haha, I was learning about Linux filesystem trees trying to understand how Moo fits.
[1nickt]: I just got my feet wet with some DBIC coding this last week. I feel "meh" about it so far. It was pretty easy to add a sub to the DBIC class to filter search results ...
[1nickt]: ... in the calling code ... $rs = $rs->maturity(" stable");. But I don;t enjoy feeling that far away from the SQL and the DB, really.
[1nickt]: The same project (CPAN Testers API) is using experimental signatures (and therefore 5.024) ... which is what replaced Method::Signatures , afaict.
[1nickt]: But the new $job is on an older Perl and therefore using M::S, the syntax of which looks clunky to me, so far.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-11-24 13:31 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (349 votes). Check out past polls.