Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

problem using regexp

by artifact (Novice)
on Jul 27, 2012 at 20:09 UTC ( [id://984090]=perlquestion: print w/replies, xml ) Need Help??

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

how do i match a particular alphabet using regexp? I cannot print the second occurrence of 'a' in my string in the following piece of code,as a beginner I cannot find where the problem is and why it is happening?

#! usr/bin/perl use warnings; use strict; my @vars; $_='1: A a A silly a sentence a (495,a) *BUT* one a which is a will be + useful. (3)'; if (@vars= /(\s)(a)(\s)/ig){ print "the text matches the pattern '\$pattern'.\n"; foreach (@vars) { print "Group: $_\n"; } } else { print " it was not found. \n"; } print "@vars\n";

Replies are listed 'Best First'.
Re: problem using regexp
by AnomalousMonk (Archbishop) on Jul 28, 2012 at 01:33 UTC
    if (@vars= /(\s)(a)(\s)/ig){ ... }

    Another approach to handling overlapping matches (and, IMHO, better because less error-prone and better integrated into the general regex expression than the manipulation of pos) is the use of a look-ahead assertion to wrap capturing groups: see  "(?=pattern)" in the Look-Around Assertions sub-section of perlre Extended Patterns. Replacing
        /(\s)(a)(\s)/ig
    with
        /(?=(\s)(a)(\s))/ig
    in the code quoted above from the OP produces the desired capture (tested).

      thanks for replying so fast ...and also it encouraged me to find different techniques.
Re: problem using regexp
by choroba (Cardinal) on Jul 27, 2012 at 20:30 UTC
    The first match eats up the space. The second 'a' therefore is not matched, because there is no space before it. If you do not want the space to be eaten, you can use a look-around:
    /\s(a)(?=\s)/ig
      thanks ,it works!!
Re: problem using regexp
by Grimy (Pilgrim) on Jul 27, 2012 at 20:36 UTC

    The problem here is overlapping matches. The space between the first and the second occurence of /a/i is part of both possible matches. Since it is gobbled by the first match, the second /a/i doesn't match.

    It is possible to "rewind" your regex by assigning to pos. See 'pos' in perldoc.

    #! usr/bin/perl use strict; use warnings; $_=' A a A '; while (/(\s)(a)(\s)/ig) { print "The text matches the pattern '$&' at position " . pos() . " +.\n"; # uncomment this to rewind #pos() = pos() - 1; }
      it works!! it also made me learn few other concepts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found