in reply to Regex: finding all possible substrings
AnomalousMonk: I enjoy your regexes! Anyway, here's another solution (am reading "all possible substrings" as "all possible substring matches"):
use Modern::Perl; my @userDefined = qw(AAA AAC ACA CAA); my $string = 'AAAAA AAACACA CAACAAA'; foreach my $element (@userDefined) { my ( $found, $elemLen ) = ( 0, length $element ); for ( my $i = 0 ; $i < length($string) - 2 ; $i++ ) { $found++ if substr( $string, $i, $elemLen ) eq $element; } say "$element: $found"; }
Output:
AAA: 5 AAC: 2 ACA: 3 CAA: 2
|
|---|