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

Re: Regex fun

by AnomalousMonk (Archbishop)
on Dec 15, 2009 at 15:43 UTC ( [id://812902]=note: print w/replies, xml ) Need Help??


in reply to Regex fun

Here's an approach that avoids the scary  (?{ code }) and  (??{ code }) regex constructs, but whether it's more readable than your  while loop is another question.

>perl -wMstrict -le "my $s = '_+0GAA__+1GAA__+2GGAA__+3GGGAA_'; print qq{'$s'}; $s =~ s{ ( \+ (\d+) [ACGT]+ ) } { (my $r = $1) =~ s{ \+ \d+ [ACGT]{$2} }{}xms; $r }xmsge; print qq{'$s'}; " '_+0GAA__+1GAA__+2GGAA__+3GGGAA_' '_GAA__AA__AA__AA_'

Note that the  '+0ACGT' sequence is handled in a way that seems consistent with your original regex, but that I don't know to be correct.

Update: Here's a version using substr that might be a bit more readable.

>perl -wMstrict -le "my $s = '_+0GAA__+1GAA__+2GGAA__+3GGGAA_'; print qq{'$s'}; $s =~ s{ ( \+ (\d+) [ACGT]+ ) } { substr $1, 1 + length($2) + $2 }xmsge; print qq{'$s'}; " '_+0GAA__+1GAA__+2GGAA__+3GGGAA_' '_GAA__AA__AA__AA_'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://812902]
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-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found