By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

RegEx question

P: 27
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
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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 2.5K+
P: 4,059
and for case insensitive matching use the "i" option:

Expand|Select|Wrap|Line Numbers
  1. $var =~ /PME/i
Aug 26 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
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 2.5K+
P: 4,059
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 2.5K+
P: 3,503
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.