Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regex Pipe Return

by TapTapTaperoo (Initiate)
on Dec 27, 2012 at 00:58 UTC ( #1010434=perlquestion: print w/ replies, xml ) Need Help??
TapTapTaperoo has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am matching a smaller string in a large string using several pipes ie:

my $string = XXXXXXXXXABXXXXXXX

m/(A|B)(A|B)/

for matching AA, AB, BA, BB

I want to keep these matches in the context of the larger string, while still being able to distinguish matches. Is there a way to substitute and return the successful event ie:

XXXXXXXXXXXabXXXXXXXXX

When I try to substitute it returns indistinguishable for the original hit ie:

XXXXXXXXXX(A|B)(A|B)XXXXXXXXX

Thank you!

Comment on Regex Pipe Return
Re: Regex Pipe Return
by roboticus (Canon) on Dec 27, 2012 at 01:24 UTC

    TapTapTaperoo:

    Sure ... use a character class (or non-capturing grouping). Something like:

    my $string = 'xxxxxxabxxx'; print $1 if $string = /(x*[ab][ab]x*)/;

    should work.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Regex Pipe Return
by LanX (Canon) on Dec 27, 2012 at 01:34 UTC
Re: Regex Pipe Return
by graff (Chancellor) on Dec 27, 2012 at 01:41 UTC
    I'm not sure what you're asking for here:

    I want to keep these matches in the context of the larger string, while still being able to distinguish matches. Is there a way to substitute and return the successful event...

    If "keep these matches in the context of the larger string" means "don't alter the string", then you wouldn't do a substitution; just do a match (with capturing parens), and assign the return value of the match to an array:

    my @strings = qw/XXXXXXXXXABXXXXXXX XXXXXXXBAXXXBBXXXX BBAABAABBXBX/; for my $string ( @strings ) { my @matches = ( $string =~ /([AB]{2})/g ); printf( "Found %d matches in %s: %s\n", scalar @matches, $string, join( ', ', @matches )); }
    Perhaps you'd like to mention what you're really trying to accomplish?
Re: Regex Pipe Return
by BrowserUk (Pope) on Dec 27, 2012 at 02:12 UTC
    When I try to substitute it returns indistinguishable for the original hit ie:

    Que?

    Um. I cannot even guess as to what you mean here?

    Why not post your code showing exactly what result you're getting; and what you want to get. Making us guess what you are trying to do is unlikely to encourage us to help you.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Regex Pipe Return
by BillKSmith (Chaplain) on Dec 27, 2012 at 03:00 UTC
    I think that you want to return the matched characters.
    use strict; use warnings; my $string = 'XXXXXXXXXXABXXXXXXXXX'; my ($match) = $string =~ m/X{10}([AB]{2})X{9}/; print $match;

    OUTPUT:

    AB

    UPDATE: Added Output

    Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2014-10-25 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls