using perl's match m/ in  a if condition

Posted on 2011-10-10
Last Modified: 2012-05-12
using the m/ to match multiple options..
my $fallQ = func1();
# $fallQ could be any word, but i want a if condition which succeds only if the $fallQ is either ASM or ac or per. Alsothe match should be case insensitive..

i tried if ($fallQ =~ m/[AC|ASM|PER]/i) {
  #do important stuff;
But this did not work
Question by:pvinodp
    LVL 13

    Expert Comment

    by:Carl Bohman
    if ($fallQ =~ m/(AC|ASM|PER)/i) ...
    LVL 13

    Accepted Solution

    The problem is that you are using square brackets [] which are used for character classes.  You need to use parentheses () to specify multiple options to match.

    Note that the regex I gave will match any text that contains the given characters anywhere in the string.  To limit to only those exact strings, you need to anchor the match using carat ^ and dollar sign $:

    if ($fallQ =~ m/^(AC|ASM|PER)$/i) ...
    LVL 26

    Assisted Solution

    Or, even better:

    if ($fallQ =~ m/^(?:AC|ASM|PER)$/i) ...

    Parens capture by default - adding ?: to the beginning of the parens will make it a non-capturing paren group.  Capture wastes resources if it is not necessary - however, unless this line will get executed many times, you'll never notice the difference (unless doing ms timings).
    LVL 13

    Assisted Solution

    by:Carl Bohman
    @wilcoxon: Agreed.  I normally do that by default, but was not certain whether the data would be used somewhere in the if block or not.  If it is needed in the block, capturing parentheses (...) will be necesary instead of non-capturing parentheses (?:...).

    Author Closing Comment

    Thanks all

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now