siva.snimmaka has asked for the wisdom of the Perl Monks concerning the following question:

HI Monks. I need a regular expression to match abcd_efgh-1.2.3 but it should not match abcd_efgh-1.2.3-hjik-3.4.5.tar.gz Please help me on this..... Thanks in Advance, SIN

Replies are listed 'Best First'.
Re: Need help on regexp
by kcott (Archbishop) on Aug 03, 2013 at 15:49 UTC

    G'day siva.snimmaka,

    Welcome to the monastery.

    "I need a regular expression to match abcd_efgh-1.2.3 but it should not match abcd_efgh-1.2.3-hjik-3.4.5.tar.gz"

    You really need to post more information to get a reasonable answer. To highlight this, the following regular expression meets your posted requirements:

    /3$/

    Does your problem really only involve just those two strings? Did you try anything yourself and ran into difficulties? A better question will get you better answers and guidelines for doing this can be found in: How do I post a question effectively?.

    -- Ken

Re: Need help on regexp
by Laurent_R (Canon) on Aug 03, 2013 at 15:02 UTC

    You might try this:

    /abcd_efgh\-1\.2\.3$/

    It will match your wanted string and not the other one. But it may not be what you are looking for, possibly you want the regex to be more generic. But you don't give enough details on what you really need.

    May be you rather need this:

    /\w{9}\-\d(\.\d){2}$/

    which will match many other strings, but still not the one you don't want. Please give more details on what you need.

      Laurent_R Your RE will not be a match string "abcd_efgh-1.2.3" from a string "abcd_efgh-1.2.3-hjik-3.4.5.tar.gz".

      Attention to the "end of string"

        I don't understand what you mean. Both my regexes match the string you want to match and don't match the string you don't want to match.

        ups.. we understand the problem differently
Re: Need help on regexp
by 2teez (Vicar) on Aug 03, 2013 at 20:28 UTC

    Hi siva.snimmaka,
    If your intention is to pick out "abcd_efgh-1.2.3" from "abcd_efgh-1.2.3-hjik-3.4.5.tar.gz", you can used

    $_ = 'abcd_efgh-1.2.3-hjik-3.4.5.tar.gz'; print $1 if /(.+?)-[a-z].+?/i; ## prints abcd_efgh-1.2.3
    or any of the previous solutions.
    However, if your intention is to "match" only "abcd_efgh-1.2.3" in data that has several lines of similar strings, then I will go with kcot's solution.
    Please also check up perlrequick and perlre if you have not.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      "... then I will go with kcot[t]'s solution."

      OK, that got a bit of a chuckle as /3$/ was really intended as a non-solution to highlight the lack of information provided in the question. :-)

      Actually, while the strings appear highly contrived, abcd_efgh-1.2.3 looks rather like the directory I might get when unpacking a tarball of v1.2.3 of the "abcd_efgh" application. If that's the case, then using the -d file test operator might be more appropriate than trying to craft a regex; but, until more information is forthcoming, who knows.

      -- Ken

        Hi kcott,
        OK, that got a bit of a chuckle as /3$/ was really intended as a non-solution to highlight the lack of information provided in the question...

        Sorry, I misunderstood your write up. But I kind of see a solution in it, that was why I mentioned it.
        Thinking if what the OP has is a file that contain say

        abcd_efgh-1.2.3 abcd_efgh-1.2.3-hjik-3.4.5.tar.gz
        then simply doing this:
        while (<DATA>){ print $_ if /3$/; }
        prints abcd_efgh-1.2.3, and thanks to if /3$/; of course using ONLY these OP strings. It could be a different thing if there are other strings ending with "3".
        All that been said on the assumption that, that was what was intended by the OP.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Need help on regexp
by 0day (Sexton) on Aug 03, 2013 at 14:56 UTC
    $_ = "abcd_efgh-1.2.3-hjik-3.4.5.tar.gz"; /^(\w+-(?:\d|\.)+)[$-]?/; print $1;