Developer
Developer

Reputation: 8636

Regex to remove consecutive characters

Need a regular expression to replace the following sequence

Before : abbbccdd   After : abcd

And also if numeric data is present instead of alphabets i would like to remove the duplicates and display

Upvotes: 2

Views: 2330

Answers (2)

vladh
vladh

Reputation: 49

This works in PHP:

preg_replace('/(.)\1+/','$1',$str);

I'm not sure what you mean with your second question, though.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838186

For the first part, in most languages you can do something like replacing (.)\1+ with $1.

The exact syntax depends on the language and the regular expression engine you are using, so check the manual for your language for more details.

Upvotes: 5

Related Questions