in reply to Identifying the position of match using regular expressions

You can use the index function. For example:

>perl -Mstrict -we "my $s = q[ABCDFEFFAA]; print index($s, $1), qq[\n] + if $s =~ /(AA)/;" 8

Hope that helps,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: Identifying the position of match using regular expressions
by Anonymous Monk on Sep 15, 2012 at 06:21 UTC
    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

      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