use the pos function.Returns the offset of where the last m//g search left off for the variable in question ($_ is used when the variable is not specified). Note that 0 is a valid match offset. undef indicates that the search position is reset (usually due to match failure, but can also be because no match has yet been run on the scalar). while (<DATA>) {
/\d/g;
say pos;
}
__DATA__
AB1CDEFGHIJKLM
ABCDEFGHIJKLM7
ABCDEF7GHIJKLM
ABC33DEFGHIJKLM
ABCDEFGH123IJKLM
ABCDEFGHIJKLM
12ABCDEFGHIJKLM
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [Watch: Dir/Any] [d/l] [select] |
if you are using the /g modifier in your regex, you can use pos($string) to get the position after the match.
generally you can use the @- and @+ arrays to get all match offsets and ends.
my $s = "ABCDFEFFAA";
$s =~ m/AA/;
say "$-[0] $+[0]";
__END__
8 10
see perlvar | [reply] [Watch: Dir/Any] [d/l] [select] |
>perl -Mstrict -we "my $s = q[ABCDFEFFAA]; print index($s, $1), qq[\n]
+ if $s =~ /(AA)/;"
8
Hope that helps,
Athanasius <°(((>< contra mundum
| [reply] [Watch: Dir/Any] [d/l] |
Thank you. But it gives some error like this
syntax error at -e line 1, near "my ="
syntax error at -e line 1, near "index(,"
Execution of -e aborted due to compilation errors.
More over i have a large data set which cannot be worked out with command line. i just gave an example of what i want. my actual data it has to identify the pattern ASHGFDF in a substring of 250 characters. i can post the pgm if needed
| [reply] [Watch: Dir/Any] |
When running a Perl one-liner on a non-Windows system you will probably need to change the double quotes (") to single quotes ('). Check the requirements of the particular shell you’re using to run perl.
But I only used a one-liner as a quick way to demonstrate the technique you can use. Once you have the match (say, in $1), you can find where in the original string it matched using index. This will work regardless of the number of characters in the string or in the match.
Note that the values returned by index are zero-based; that is, if the match begins on the first character of the original string, index returns zero. If the substring is not found within the original string, index returns -1.
Athanasius <°(((>< contra mundum
| [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |