Is this the regular expression you're looking for?
/([\s.'-,])\1+/
OK, now that will fit. If you use Perl, you can replace it using the following expression:
s/([\s.'-,])\1+/$1/g
Edit: if you use: um: PHP, then you should use this syntax:
$out = preg_replace('/([\s.\'-,])\1+/', '$1', $in);
The group () matches the character, and \1 means that the same thing that it just matched in parentheses happens at least once more. In the replacement, $1 refers to a match in the first set of parentheses.
Note. This is Perl compatible regular expression syntax (PCRE).
On the perlretut page:
Repetition Matching
The examples in the previous section demonstrate annoying weakness. We used only 3-letter words or pieces of words of 4 letters or less. We would like to be able to match words or, in a more general sense, strings of any length without writing out tedious alternatives such as \w\w\w\w|\w\w\w|\w\w|\w .
It was in this matter that the quantizer metacharacters were created ? , * , + and {} . They allow us to limit the number of repetitions for part of the regular expression, which we consider a coincidence. Quantifiers are placed immediately after the character, character class, or grouping we want to specify. They have the following meanings:
a? means: match 'a' 1 or 0 times
a* means: match 'a' 0 or more times, i.e. any number of times
a+ means: match 'a' 1 or more times, i.e. at least once
a{n,m} means: match at least "n" times, but no more than "m" times.
a{n,} means: match at least "n" or more
a{n} means: match exactly "n" times
amphetamachine
source share