RegEx question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gotejjeken
    New Member
    • Sep 2007
    • 27

    RegEx question

    I have a string of characters "PME". I only want the string to be valid if it matches PME exactly (in that order) and must be exactly three letters long (case insensitive). In short, only the string PME will work, no variations, and no shorter.

    I have this so far:

    Code:
    [p(?=m(?=e))P(?=M(?=E))]
    This will ensure that PME is in the correct sequence, but something like PM works also which I can't have. I tried:

    Code:
    [p(?=m(?=e))P(?=M(?=E))]{1}
    However that doesn't seem to work.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    If you are checking to see if PME is in the string, why not use the following:

    Code:
    $var =~ /PME/;
    That will match "PME", exactly as you have prescribed.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      and for case insensitive matching use the "i" option:

      Code:
      $var =~ /PME/i

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        But the string must be ONLY "PME", not just contain "PME" - a.k.a. "blah PME foo" shouldn't match, if I'm understanding correctly.

        If so, don't bother with a RegEx: just use the lexical equality operator, eq:

        [CODE=perl]if ($string eq "PME") {
        # ...
        }[/CODE]

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by Ganon11
          But the string must be ONLY "PME", not just contain "PME" - a.k.a. "blah PME foo" shouldn't match, if I'm understanding correctly.

          If so, don't bother with a RegEx: just use the lexical equality operator, eq:

          [CODE=perl]if ($string eq "PME") {
          # ...
          }[/CODE]

          You are totally correct. No regexp is required per his original requirements, must be PME and only PME, so using a string operator (eq) instead of a regexp is the ticket.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by KevinADC
            You are totally correct. No regexp is required per his original requirements, must be PME and only PME, so using a string operator (eq) instead of a regexp is the ticket.
            Or, if you are really anal and truly want to use a regex, you could do this:

            Code:
            $var =~ /^PME$/i
            Maybe its just me, but I like regex's and just had to throw that into the mix.

            Regards,

            Jeff

            Comment

            Working...