Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: cleaving a sequence with specific alphabets

by tachyon-II (Chaplain)
on May 21, 2008 at 11:31 UTC ( [id://687751]=note: print w/replies, xml ) Need Help??


in reply to Re: cleaving a sequence with specific alphabets
in thread cleaving a sequence with specific alphabets

Probably not as good as the split solution but perhaps a little easier to read if you recognise the idiom:

@arr = $string =~ m/(.*?[KR])(?!P)/gs;

Replies are listed 'Best First'.
Re^3: cleaving a sequence with specific alphabets
by mwah (Hermit) on May 21, 2008 at 12:10 UTC

    You won't need capturing parentheses when evaluating in list context. Afaik does the .*? invoke a speed penalty, so that the expression might be optimized as:

    ... my @arr = $string =~ / [^KR]+ # collect non K|R . # the following must be K|R (?!P) # ignore if fragment would start by P /gsx; ...

    Regards

    mwa

      This is broken, not optimised. The .*? was there for a reason.

      $string = "RYOURPOSTBROKEN";
        That snippet shows that yours is broken too. It doesn't return the last segment ("EN").
        my $string = "RYOURPOSTBROKEN"; print("input: $string\n"); print("expecting: R YOURPOSTBR OK EN\n"); print("\n"); { # mwah (split) my $re = qr/(?<=[KR])(?!P)/; my @arr = split $re, $string; print("split $re: @arr\n"); } { # tachyon-II (re) my $re = qr/(.*?[KR])(?!P)/s; my @arr = $string =~ /$re/g; print("$re: @arr\n"); } { # mwah (re) my $re = qr/[^KR]+.(?!P)/s; my @arr = $string =~ /$re/g; print("$re: @arr\n"); }
        input: RYOURPOSTBROKEN expecting: R YOURPOSTBR OK EN split (?-xism:(?<=[KR])(?!P)): R YOURPOSTBR OK EN (?s-xim:(.*?[KR])(?!P)): R YOURPOSTBR OK (?s-xim:[^KR]+.(?!P)): YOU POSTBR OK EN (?s-xim:[^KR]*.(?!P)): R YOU POSTBR OK EN

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found