in reply to Re: Why my Regex doesn't work
in thread Why my Regex doesn't work

The OP wanted CCHHHHHHHCC, not CCHHHHHHHHC. Maybe that's a typo, or maybe he wants all the surrounded characters substituted simultaneously, as in a cellular automaton. In that case,
s/(?<=(\w))(?!\1).(?=\1)/$1/g;

Replies are listed 'Best First'.
Re^3: Why my Regex doesn't work
by Not_a_Number (Prior) on Apr 11, 2017 at 17:25 UTC

    Yeah. The problem seems underspecified.

    For example, what output would the OP want from CHCHC?

    AnomalousMonk's snippet outputs CCCHC, while anonymonk's gives CCHCC...

Re^3: Why my Regex doesn't work
by flappygoat (Initiate) on Apr 11, 2017 at 17:36 UTC
    thank you so much! That is exactly what I needed. I wonder tho if it's possible to do without "lookaround".
      I wonder tho if it's possible to do without "lookaround".

      There is probably some tricky way to avoid lookarounds, but just as a matter of curiosity, why would you want to? Lookarounds have been a part of Perl 5 regex from the beginning. Was it only a matter of curiosity on your part?


      Give a man a fish:  <%-{-{-{-<

        yes just curiosity. I only recently started learning perl. Before I posted this question I didn't even knew lookarounds existed. Now I did a bit of reading about them and cool. If I knew about them I would have probably solved this myself. So I'm just curious if one does not know about lookarounds is this problem solvable. It would probably be very convoluted as I'm starting to think that regex is not always best for stirng matching and changing?