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

Hello,
Is there a way I can make use of s/// to remove all repeating characters in any given string , in place?
so $str = "aaabbcdeffgh" should be "cdegh" or any other way to do this ?
  • Comment on How to remove all repeating characters in a string in place

Replies are listed 'Best First'.
Re: How to remove all repeating characters in a string in place
by mr_ron (Deacon) on Jan 30, 2016 at 23:29 UTC
Re: How to remove all repeating characters in a string in place
by Cristoforo (Curate) on Jan 30, 2016 at 23:33 UTC
    This substitution will give the results you want.

    $str =~ s/(.)\1+//g;

    Update: Redid after seeing mr_ron's post. (was originally s/((.)\2+)//g;)

      Thanks a lot for the help.
      Yes this works , I was trying to do s/(\w)$1+//;
      I forgot that $1 is not defined inside a pattern as it is being matched and hence I should be using \1 instead
        ... I was trying to do s/(\w)$1+//; ...

        Had you given this information in addition to the info you gave in the OP, it would have been most helpful in addressing your problem. Please always try to give some indication of what you have tried, how it failed, what you expected, etc. Please see How do I post a question effectively?.


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

Re: How to remove all repeating characters in a string in place
by GotToBTru (Prior) on Jan 30, 2016 at 22:49 UTC

    What have you tried so far?

    There are many ways to do this, most of which are documented in any tutorial about regular expressions. Try Regular Expressions Tutorial, the Basics (for BEGINNERS). After a few paragraphs, there is the section entitled Substitution Operators, and that should tell you what you need to know.

    Update: the following section on character classes will also be useful, I think.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: How to remove all repeating characters in a string in place
by Discipulus (Canon) on Jan 30, 2016 at 22:50 UTC
    Yes, alternance operator in regex | and the global regex modifier g
    perl -e "$ARGV[0]=~s/a|b|f//g;print $ARGV[0]" aaabbcdeffgh cdegh

    The s///g behaviour is introduced in perlrequick's Search and Replace part and in the Global Matching part of perlretut

    HtH

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to remove all repeating characters in a string in place
by 2teez (Vicar) on Jan 30, 2016 at 23:26 UTC

    Hi,
    ...or any other way to do this ?

    What about using tr/// you can do perldoc -f tr on your CLI to read the documentation.
    The following works like you wanted:

    perl -we "$ARGV[0]=~tr[abf]()ds; print $ARGV[0]" aaabbcdeffgh # prin +ts cdegh

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me