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

Regular Expression - drive me crazy

P: 13
I am trying to do a pattern matching for some setup being done previously by others but I thing I got it all wrong. Is there a simpler way of doing this?
For example:
PATTERN: J??[!G]?[!R,!L]EB*[E,H,K,W,L] means
start with J
2nd & 3rd & 5th characters are wildcard matches 1
4th character is not a "G"
6th character is not an "R" or "L"
7th-8th character match "EB"
9th character is a wildcard matches zero or more
follow by a match of either E, H, K, W, or L.
Expand|Select|Wrap|Line Numbers
  1. my $pattern = "J??[!G]?[!R,!L]EB*[E,H,K,W,L]"; 
  2. my $data1 = "JMNPAVEBL";  # should match with the pattern 
  3. $pattern =~ s/\,/\|/g; 
  4. $pattern =~ s/\*/\.\*/g; 
  5. $pattern =~ s/\?/\./g; 
  6. while ($pattern =~ /\[\!.*\]/) { 
  7.     $pattern =~ s/\[\!(.*)\]/\(\?\!\[\#\]\)/; #something is really wrong here!
  8.     my $subpattern = $1; 
  9.     $subpattern =~ s/\!//g; 
  10.     $pattern =~ s/#/$subpattern/; 
  11. }
  12. print "Trying to match $data1 with $pattern\n";
  13. if ($data1 =~ /$pattern/i) { 
  14.     print "Found a match!\n"; 
  15. else { 
  16.     print "No matching found!\n"; 
  17. }
Nov 18 '08 #1
Share this Question
Share on Google+
3 Replies


nithinpes
Expert 100+
P: 410
The pattern for your requirement will be something like:
Expand|Select|Wrap|Line Numbers
  1.    /J..[^G].[^RL]EB.*[EHKWL]/
  2.  
Note that '.' will match any character. If you want it to be alphabetic only use [A-Z] instead.
The requirement for 9th character is not very clear-"9th character is a wildcard matches zero or more", Zero or more of what?
However, it would be good if you try to understand regular expressions before posting any further question.

To understand regular expressions better, consider going through perlre.
Nov 18 '08 #2

P: 13
Hi nithinpes,

The 9th character is a wildcard zero or more of anything. My confusion is to detect the !G and !R,!L. I didn't know it's translated into [^G] and [^RL]. I thought ^ is for beginning of string. Now I know it can be used to negate the character class. Please forgive my ignorance and thanks for the solution!
Nov 18 '08 #3

nithinpes
Expert 100+
P: 410
Hi nithinpes,

The 9th character is a wildcard zero or more of anything. My confusion is to detect the !G and !R,!L. I didn't know it's translated into [^G] and [^RL]. I thought ^ is for beginning of string. Now I know it can be used to negate the character class. Please forgive my ignorance and thanks for the solution!
^ is for beginning of string. But when it it used within character class [], it stands for negation.
Nov 18 '08 #4

Post your reply

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