regex - Regexp not matching when the string contains a space -
i have following rule match pattern in string.
2 alphanumeric characters, followed 0 or 1 alphabets, followed 0 or more spaces, followed 1 4 digits
i tried regexp, still missing out few cases.
here code:
#!/usr/bin/perl use strict; use warnings; @quer = ('a1q 1234', '11 asdd', 'as 11aa', 'asdasdasd', 'asdd as', 'asdasd asdassdasd', '11 1231', '11 12345', '345 1 1231', '12a 123', 'ab 12', 'ab12'); foreach $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 matching ab 12
not ab12
, according rule, should match both.
you having word boundary in middle, screwing 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
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}
same [a-za-z]?
Comments
Post a Comment