Reputation: 1342
I have the following rule to match the pattern in a string.
2 alphanumeric characters, followed by 0 or 1 alphabets, followed by 0 or more spaces, followed by 1 to 4 digits
I tried a regexp, but I am still missing out few cases.
Here is my code:
#!/usr/bin/perl
use strict;
use warnings;
my @quer = ('a1q 1234', '11 asdd', 'as 11aa', 'asdasdasd', 'asdd as', 'asdasd asdassdasd', '11 1231', '11 a 12345', '345 1 1231', '12a 123', 'ab 12', 'ab12');
foreach my $query (@quer) {
if ($query =~ m/\b[a-zA-Z0-9]{2}[a-zA-Z]{0,1}\s*\b[0-9]{1,4}\b/) {
print "Matched : $query\n";
} else {
print "Doesn't match : $query\n";
}
}
My code is matching ab 12
but not ab12
, but according to the rule, it should match both.
Upvotes: 0
Views: 132
Reputation: 1321
if ($query =~ m/[0-9A-z]{2}[A-z]?\s*[0-9]{1,4}$/)
This above code also will work.
Upvotes: 0
Reputation:
In perl (and some other languages) you have some nice shortcuts for alphanumeric, numbers and stuff like that.
e.g:
\w Match "word" character (alphanumeric plus "_")
\W Match non-word character
\s Match whitespace character
\S Match non-whitespace character
\d Match digit character
\D Match non-digit character
But your problem is the word boundary at the middle (\b
)
try this:
if ($query =~ m/\b\w{2}\w?\s*\d{1,4}\b/)
Upvotes: 1
Reputation: 1821
Try this:
if ($query =~ m/\b[a-zA-Z0-9]{2}[a-zA-Z]{0,1}\s*[0-9]{1,4}\b/) {
It is doing exactly what u asked for!!!
Upvotes: 2
Reputation: 213223
You are having a word boundary in the middle, which is screwing your regex. Remove it:
if ($query =~ m/\b[a-zA-Z0-9]{2}[a-zA-Z]{0,1}\s*\b[0-9]{1,4}\b/)
^
remove this
should be:
if ($query =~ m/\b[a-zA-Z0-9]{2}[a-zA-Z]?\s*[0-9]{1,4}\b/)
Note, [a-zA-Z]{0,1}
is same as [a-zA-Z]?
Upvotes: 3