Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to get Incremental charecter groups in a string

by japhy (Canon)
on Jun 19, 2006 at 11:31 UTC ( [id://556230]=note: print w/replies, xml ) Need Help??


in reply to How to get Incremental charecter groups in a string

That's pretty tricky to do with regexes, but I'll show you anyway:
while ($str =~ m{ ( # capture to $1 . # any character (?: # this chunk: $ # either the end of the + string | (??{ chr(1 + ord substr($&, -1)) }) # or the next character + (ASCII-wise) after # the one we just match +ed )+ # one or more times ) # end capture }xgs) { print "run: <$1>\n"; }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: How to get Incremental charecter groups in a string
by liverpole (Monsignor) on Jun 19, 2006 at 12:46 UTC
    japhy,

    It's a slick regex, but you should be aware there's a problem running it with warnings:

    #!/usr/bin/perl + use strict; use warnings; + my $str = "this is abc and bcf and xyz and ijklmn"; + while ($str =~ m{ ( # capture to $1 . # any character (?: # this chunk: $ # either the end of the + string | (??{ chr(1 + ord substr($&, -1)) }) # or the next character + (ASCII-wise) after # the one we just match +ed )+ # one or more times ) # end capture }xgs) { print "run: <$1>\n"; }

    Produces:

    (?: # this chunk: $ # either the end of the + string | (??{ chr(1 + ord substr($&, -1)) }) # or the next character + (ASCII-wise) after # the one we just match +ed )+ # one or more times matches null string many times in regex; marked by <-- HERE in m/ ( # capture to $1 . # any character (?: # this chunk: $ # either the end of the + string | (??{ chr(1 + ord substr($&, -1)) }) # or the next character + (ASCII-wise) after # the one we just match +ed )+ # one or more times <-- HERE ) # end capture / at ./x line 17. run: <hi> run: <abc> run: <bc> run: <xyz> run: <ijklmn>

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Yeah, I know, I got it too. A pox on it.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-20 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found