http://qs321.pair.com?node_id=292769


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

    Ahh, not a horrible idea for simple patterns. I would need to add a ^ at the beginning and maintain a $ at the end of the RE. For a simple enough pattern I could even generate the partial REs programatically. Alas, it would be tougher to deal with quantifiers, capturing groups, etc, and, alas, I am quite the fan of quantifiers.

    (It took me a while to recognize what you were suggesting. I didn't notice the ".=" in $tre .= shift @tre.)