Regular Expressions 101

Community Patterns

Find repeating adjacent characters in a string

1

Regular Expression
PCRE (PHP <7.3)

Description

In an interview, I was asked to remove all repeating adjacent characters in a string except one. For example, input : abbccasdjajksdhkjaaaaa output : abcasdjajksdhkja

In Java, the syntax will be s.replaceAll("(.)\\1{1,}", "$1");

where s is the input string.

Submitted by anonymous - 4 years ago