Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Identifying the position of match using regular expressions

by Anonymous Monk
on Sep 15, 2012 at 06:01 UTC ( [id://993839]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi,
i am searching for a pattern with in 10 characters and if it is found i want to get the position where it has been found with in that 10 characters.
for example if i have to find AA in ABCDFEFFAA, my pgm is able to recognize that there is a match but i want at which position it is acheving the match
can any oe tell me how?

Replies are listed 'Best First'.
Re: Identifying the position of match using regular expressions
by CountZero (Bishop) on Sep 15, 2012 at 08:10 UTC
    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
Re: Identifying the position of match using regular expressions
by tinita (Parson) on Sep 15, 2012 at 10:59 UTC
    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
Re: Identifying the position of match using regular expressions
by Athanasius (Archbishop) on Sep 15, 2012 at 06:10 UTC

    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

      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

Re: Identifying the position of match using regular expressions
by clueless newbie (Curate) on Sep 15, 2012 at 13:48 UTC

    Look at the special variables @- and @+.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://993839]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-28 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found