in reply to Matching against a partially known string
If the patterns are all simple, one approach would be to represent each pattern as a list of atoms, and test whether a partial pattern can consume the buffer:
my @re= qw( [abc] b+ c ); my @tests = qw( a ab ba bb bbbbbcdef bbbbbbg ); TEST: foreach my $buff ( @tests ) { my @tre = @re; my $tre = ""; while (@tre > 1) { $tre .= shift @tre; if ( $buff =~ m/^$tre$/ ) { print "Test string '$buff' matched partial pattern, $tre\n +"; next TEST; } } $tre .= shift @tre; if ( $buff =~ m/^$tre/ ) { print "Test string '$buff' matched whole pattern, $tre\n"; } else { print "Test string '$buff' did not match pattern $tre\n"; } }
This method will fail if you use alternation, or atoms with a minumum length > 1, such as "a{5}".
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Matching against a partially known string
by wonkozen (Initiate) on Sep 20, 2003 at 15:51 UTC |
In Section
Seekers of Perl Wisdom