Help with regular expression please?
am 10.10.2007 22:17:21 von b-patton
Here is the simplified code I have attempted. It failed me by finding 31ZB1 to be the same as 31ZB11
__BEGIN__
$focusRule = '31ZB1';
# match v v v v
@l = ( qw ( 31ZA1 31ZB1 31ZB11 31ZB15 175A2 31ZU7 31ZB 31ZB1a 31ZB1b ));
# n = number
# a = alpha char
# + is one or more
# ? one or more may or may not be there
# the rule names are built
# n+a+n?a?n?
# so the possible correect permutations are
# n+a+
# n+a+n+
# n+a+n+a+
# n+a+n+a+n+
# 31ZB1 31ZB 31ZB1a 31ZB1b1 all these should match $focusRule
# 31ZB1 and 31ZB11 are different
# I also need to set $focusRule to '31ZB' and have it find the same set.
$found = 0;
foreach $rule (@l ) {
my $a1 = ($rule =~ /^$focusRule[abc123]?$|,$focusRule[abc123]/) ? 1 : 0;
my $a2 = ($focusRule =~ /^$rule[abc123]?/) ? 1 : 0;
print "focusRule = '$focusRule' , rule = '$rule', a1 = $a1 , a2 = $a2\n";
if ($a1 || $a2) {
print "focusRule = '$focusRule' matches '$rule'\n";
$found++;
}
}
print "found = $found\nshould have found 4";
__END__
Re: Help with regular expression please?
am 11.10.2007 02:57:42 von Bob Walton
Billy N. Patton wrote:
> Here is the simplified code I have attempted. It failed me by finding
> 31ZB1 to be the same as 31ZB11
>
> __BEGIN__
>
> $focusRule = '31ZB1';
> # match v v v v
> @l = ( qw ( 31ZA1 31ZB1 31ZB11 31ZB15 175A2 31ZU7 31ZB 31ZB1a 31ZB1b ));
> # n = number
> # a = alpha char
> # + is one or more
> # ? one or more may or may not be there
> # the rule names are built
> # n+a+n?a?n?
> # so the possible correect permutations are
> # n+a+
> # n+a+n+
> # n+a+n+a+
> # n+a+n+a+n+
> # 31ZB1 31ZB 31ZB1a 31ZB1b1 all these should match $focusRule
> # 31ZB1 and 31ZB11 are different
> # I also need to set $focusRule to '31ZB' and have it find the same set.
> $found = 0;
> foreach $rule (@l ) {
> my $a1 = ($rule =~ /^$focusRule[abc123]?$|,$focusRule[abc123]/) ? 1 : 0;
> my $a2 = ($focusRule =~ /^$rule[abc123]?/) ? 1 : 0;
> print "focusRule = '$focusRule' , rule = '$rule', a1 = $a1 , a2 = $a2\n";
> if ($a1 || $a2) {
> print "focusRule = '$focusRule' matches '$rule'\n";
> $found++;
> }
> }
> print "found = $found\nshould have found 4";
>
>
> __END__
Well, note that
'31ZB11'=~/^31ZB1[abc123]?$/;
will match. I presume from your comments (if I read them correctly)
that you thought that one would not match. Not sure why, since you
recognize that
'31ZB1a'=~/^31ZB1[abc123]?$/;
for example, will match. If you meant something else, please clarify.
Also, I don't follow your bit after the alternation symbol in your "my
$a1 ..." line. None of your attempted match strings have a comma in
them, so the second alternative will never succeed.
If you haven't already, you might try:
use re 'debug';
for useful information about complex regular expression matches.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
Re: Help with regular expression please?
am 11.10.2007 19:04:41 von nobull67
On Oct 10, 9:17 pm, "Billy N. Patton" wrote:
> # n = number
>
> # a = alpha char
>
> # + is one or more
>
> # ? one or more may or may not be there
>
You appear to be saying you are inventing your own simple regex
grammar
So the translation from your grammar to Perl's
my %translate_pattern_token = (
'n' => '\d'
'a' => '[[:alpha:]]'
'+' => '+'
'?' => '*'
);
So if a $rule is a pattern in your notation you could convert it to a
Perl regex thus...
my $regex = join '', map { $translate_pattern_token{$_} } split //,
$rule;
# E&OE
> # the rule names are built
OK now you've lost me. What did you mean by that?