siskos1 has asked for the wisdom of the Perl Monks concerning the following question:

hi. my problem is i forgot how to embed code in regex and for this particular problem i am now clueless

i couldn't do it even after i searched a lot today for hours. i want to count some characters in a string. then after a threshold, i want to save the place. for example; let my string be: "abacdbccdee". i want to count total of 3, c's and b's. (order or how many of each one is not important) so at 6th place, i count 2b's and 1c. only thing i want in this puzzle is 6

i can do it if i split string into array but if i dont remember wrong, regular expression is more efficient. and this my another concern, considering i will search big strings, is there a way to stop regular expression after i found the place. no need to look for rest

thank you in advance

Replies are listed 'Best First'.
Re: a regular expression problem
by moritz (Cardinal) on Feb 29, 2012 at 16:03 UTC

    No need to execute any code inside a regular expression to solve your problem:

    use strict; use warnings; use 5.010; # only required for say() $_ ='abacdbccdee'; if (/(?:[^bc]*[bc]){3}/g) { say pos; }
      you had answered my similar problem about 2 years ago. i returned to perl recently again and all i can say is thank you. ^^
Re: a regular expression problem
by Eliya (Vicar) on Feb 29, 2012 at 16:27 UTC

    In the general case, to execute some code (e.g. count) every time a pattern is found, you could use a while loop with m//g:

    $_ ='abacdbccdeeabacdbccdee'; my $cnt; while (/[bc]/g) { last if ++$cnt == 3; } say pos;
Re: a regular expression problem
by AnomalousMonk (Archbishop) on Feb 29, 2012 at 17:32 UTC
    ... at 6th place, i count 2b's and 1c. only thing i want in this puzzle is 6

    You should be aware that Perl strings, arrays, etc. are 0-based. So the 'bcc' substring I see in 'abacdbccdee' (and that I think you want) begins at offset 5.

    Update: Oops... I may have misinterpreted what siskos1 wants. Nevermind.

Re: a regular expression problem
by ww (Archbishop) on Feb 29, 2012 at 16:14 UTC
    • "i forgot how to"
          Strike One.
    • "but if i dont remember wrong"
          ...Strike Two!

        Strike 3: lack of effort; failure to read documents; code omission; lack of search fu (to say nothing of not bothering to check perldoc -f toc).

    siskos1, you've posted 8 writeups in the two years you've been a monk. It's time to (re- ?) read How do I post a question effectively? and company. Then, (hint) check out tr///.

      thank you for your constructive approach. i dont earn money or anything with perl. i maybe dont even know %1 of what you know about perl. it was, not quite, but sort of a hobby to solve some problems. now i need it again. not a big deal to be honest. think it like hardcore gamers vs casual gamers. by the way you seem like a great rager in multiplayer rts games. play some sokoban ^^
      A reply falls below the community's threshold of quality. You may see it by logging in.