Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

want regex to find 2nd and 3rd occurence of a character

by heidi (Sexton)
on May 22, 2008 at 18:39 UTC ( #688013=perlquestion: print w/ replies, xml ) Need Help??
heidi has asked for the wisdom of the Perl Monks concerning the following question:

hi all As i am a newbie to regex and perl, hope you will help me solve this problem. I also learn as i take the help.
I have a string like $str="AJKDHAKAESRADADKLASRRASDASDKASEKA" for this i have regex @ar=split('(?<=[KR](?!P))',$str); #to split where i ever i see a K or +R.
Now what i want to do is, 1) I want to split the string during the second occurance of K or R. and not during his first occurence. In that case, in my array, each fragment will contain two K (or) R, one being somewhere in the string and other will be at the end of string ending with a K or R. 2) Likewise, i also want to split the string during the third occurance of K or R and not during the second occurance. In that case, my array will have fragments containing three K's or R's, two being somewhere in the string and the last one will be at the end of string ending with a K or R. Please help me learn :) Regards Heidi.

Comment on want regex to find 2nd and 3rd occurence of a character
Download Code
Re: want regex to find 2nd and 3rd occurence of a character
by moritz (Cardinal) on May 22, 2008 at 18:54 UTC
    You don't want to do that with regexes.

    Regexes are very powerful, but if you try to do too much with them, the regexes grow very fast in complexity, and you end up with a piece of mess that you can't maintain.

    Use ordinary perl code to postprocess your results instead.

Re: want regex to find 2nd and 3rd occurence of a character
by toolic (Chancellor) on May 22, 2008 at 19:15 UTC
    I agree with moritz in that you might want to use another approach now that you have your string split into an array.

    This sounds like it might be a job for List::MoreUtils:

    use strict; use warnings; use List::MoreUtils qw(natatime); my $str="AJKDHAKAESRADADKLASRRASDASDKASEKA"; my @ar = split '(?<=[KR](?!P))', $str; #to split where i ever i see a +K or R. print "ar: @ar\n"; my @new; my $it = natatime 2, @ar; while (my @vals = $it->()) { push @new, (join '', @vals); } print "new: @new\n";

    prints:

    ar: AJK DHAK AESR ADADK LASR R ASDASDK ASEK A new: AJKDHAK AESRADADK LASRR ASDASDKASEK A

    Is that what you are looking for for question #1? If so, I think you can use the natatime function to also solve #2.

    Update: OP has been updated.
    Also, it is best if you just put code snippets inside <code> tags, rather than the bulk of your question.

      hi toolic... THATS BRILLIANT....thank you so much... its working perfect... Amazing .... cool module. i will start using it efficiently !!! Perl monks rocks :) Thanks :)
Re: want regex to find 2nd and 3rd occurence of a character
by CountZero (Bishop) on May 22, 2008 at 19:29 UTC
    Using split to cut up the string but capturing the delimiters so they do not get lost.

    use strict; use warnings; my $str="AJKDHAKAESRADADKLASRRASDASDKASEKA"; #split on K or R and keep the delimiters my @parts = split /([KR])/, $str; print "@parts\n\n"; # return parts in groups so each group has 2 K or R my $start = 0; foreach my $end (1 .. $#parts / 4) { print join '', @parts[$start .. ($end * 4 - 1)], "\n"; $start = $end * 4; } print "\n"; # return parts in groups so each group has 3 K or R $start = 0; foreach my $end (1 .. $#parts / 6) { print join '', @parts[$start .. ($end * 6 - 1)], "\n"; $start = $end * 6; }

    Can you see the pattern emerging?

    Or using a single regex:

    use strict; use warnings; my $str="AJKDHAKAESRADADKLASRRASDASDKASEKA"; my @parts = $str =~ m/((?:[^KR]*[KR]){2})/g; print "@parts\n\n"; @parts = $str =~ m/((?:[^KR]*[KR]){3})/g; print "@parts\n";

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      It works obsoultely fine. simple and clear thank you very much for the help.
Re: want regex to find 2nd and 3rd occurence of a character
by psini (Deacon) on May 22, 2008 at 19:29 UTC

    As moritz suggested, you can use the same split as before. Then you can join the fragments in twos or in threes as you need

    Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

Re: want regex to find 2nd and 3rd occurence of a character
by DrHyde (Prior) on May 23, 2008 at 10:03 UTC
    Instead of using split() and a complicated regex, I think I'd use index() and substr().

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-12-28 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls