http://www.perlmonks.org?node_id=961125


in reply to approximate regular expression

If you would like a non-regex brute force method.

#! C:/Perl/bin/perl use strict; use warnings; my $pattern = "JEJE"; my $string = "EJKJUJHJDJEJEJEDEJOJOJJJAHJHJSHJEFEJUJEJUJKIJS"; my @pattern_list = split //, $pattern; my $pattern_length = @pattern_list; for my $x ( 0..((length $string) - $pattern_length) ){ my $test_string = substr $string, $x, $pattern_length; my @result_array = split //, $test_string; my $score = 0; for my $y ( 0..$#pattern_list ){ $score++ if $pattern_list[$y] eq $result_array[$y]; } if( $score > 1 ){ print "String: $test_string, position: $x, score: $score\n"; } }

Results

String: JKJU, position: 1, score: 2 String: JUJH, position: 3, score: 2 String: JHJD, position: 5, score: 2 String: JDJE, position: 7, score: 3 String: JEJE, position: 9, score: 4 String: JEJE, position: 11, score: 4 String: JEDE, position: 13, score: 3 String: DEJO, position: 15, score: 2 String: JOJO, position: 17, score: 2 String: JOJJ, position: 19, score: 2 String: JJJA, position: 21, score: 2 String: JHJS, position: 26, score: 2 String: SHJE, position: 29, score: 2 String: JEFE, position: 31, score: 3 String: FEJU, position: 33, score: 2 String: JUJE, position: 35, score: 3 String: JEJU, position: 37, score: 3 String: JUJK, position: 39, score: 2