467,107 Members | 1,239 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,107 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. [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:

Expand|Select|Wrap|Line Numbers
  1. [p(?=m(?=e))P(?=M(?=E))]{1}
However that doesn't seem to work.
Aug 26 '08 #1
  • viewed: 1014
Share:
5 Replies
numberwhun
Expert Mod 2GB
If you are checking to see if PME is in the string, why not use the following:

Expand|Select|Wrap|Line Numbers
  1. $var =~ /PME/;
  2.  
That will match "PME", exactly as you have prescribed.

Regards,

Jeff
Aug 26 '08 #2
KevinADC
Expert 2GB
and for case insensitive matching use the "i" option:

Expand|Select|Wrap|Line Numbers
  1. $var =~ /PME/i
Aug 26 '08 #3
Ganon11
Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. if ($string eq "PME") {
  2.    # ...
  3. }
Aug 27 '08 #4
KevinADC
Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. if ($string eq "PME") {
  2.    # ...
  3. }

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.
Aug 27 '08 #5
numberwhun
Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. $var =~ /^PME$/i
  2.  
Maybe its just me, but I like regex's and just had to throw that into the mix.

Regards,

Jeff
Aug 29 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by engwar1@yahoo.com | last post: by
4 posts views Thread by aevans1108@yahoo.com | last post: by
2 posts views Thread by Tim Conner | last post: by
6 posts views Thread by Du Dang | last post: by
17 posts views Thread by clintonG | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Martin Evans | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.