in reply to Re^3: approximate regular expression
in thread approximate regular expression

Thanks for the suggestion

Imagine, for instance, a regexp like this

$pattern="[^D]{2,3}FGR{2}..H";

There is not fuzzyness on this. From a list of strings, some will match and some will NOT match this regular expression. The question is: considering ONLY substitutions, allow a maximum of 2 mismatches when evaluating if the string matches or not to $pattern. This will result in the migration of some strings from the 'non-matching group' to the 'matching group'.

How to do this in Perl?

Replies are listed 'Best First'.
Re^5: approximate regular expression
by Anonymous Monk on Mar 25, 2012 at 14:48 UTC

    I don't get it (I'm a different Anonymous)

    Does this help?

    #!/usr/bin/perl -- use strict; use warnings; my $pattern = "JEJE"; my $string = "EJKJUJHJDJEJEJEDEJOJOJJJAHJHJSHJEFEJUJEJUJKIJS"; my $fuzzyPattern = fuzzUpEverySecond( $pattern ); print "$pattern\n$fuzzyPattern\n"; print " $string\n"; while( $string =~ m/($fuzzyPattern)/g ){ print ' ' x ( pos($string) - length($1) -1 ), "^$1\n"; } sub fuzzUpEverySecond { my( $pat ) = @_; $pat =~ s/(.)./$1./g; $pat; } __END__ JEJE J.J. EJKJUJHJDJEJEJEDEJOJOJJJAHJHJSHJEFEJUJEJUJKIJS ^JKJU ^JHJD ^JEJE ^JOJO ^JJJA ^JHJS ^JUJE ^JUJK