Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Remove repeated characters from a string

by knew (Monk)
on May 13, 2004 at 14:49 UTC ( [id://353087]=note: print w/replies, xml ) Need Help??


in reply to Remove repeated characters from a string

This bit of code will do the job:

$string =~ s/(.)\1/$1/g

Alternatively, if you want to ignore case when comparing, you could use:

$string =~ s/(.)\1/$1/gi

And finally, if you wanted to trim any number of duplicates down to a single letter, this would work:

$string =~ s/(.)\1+/$1/gi

Replies are listed 'Best First'.
Re:^2 Remove repeated characters from a string
by Anonymous Monk on May 13, 2004 at 14:55 UTC
    the duplicates are not nesscarly beside each other
    where = wher Expressions = Expresion Finished = Finshed indicated = indcate

      This will do that for you:

      1 while $string =~ s/(.)(.*)\1/$1$2/g
        This construction with the while loop made my day. Thanks a lot, sir knew! :D

      If you had put more effort into your first question you would not now be explaining what you really meant.

       

Log In?
Username:
Password:

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

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

    No recent polls found