The trick is to reverse the string being searched:
use strict; use warnings; my @strs = qw(GNNTCGANNTT GAATCGNNNTT GANNCGNNNNN); for my $str (@strs) { my $rstr = reverse $str; my @starts; push @starts, $-[1] while $rstr =~ /(N+)/g; if (1 == @starts) { print "$str: One N found at @starts\n"; } elsif (@starts) { print "$str: Ns found at @starts\n"; } else { print "No N found in $str\n"; } }
Prints:
GNNTCGANNTT: Ns found at 2 8 GAATCGNNNTT: One N found at 2 GANNCGNNNNN: Ns found at 0 7
Note too that the special array @- is used to get the index of the first match for each run of Ns.
In reply to Re: Identifying Reverse Position of Specific Character in a String
by GrandFather
in thread Identifying Reverse Position of Specific Character in a String
by monkfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |