Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace repeating characters with one with a regex

Tags:

regex

I need a regex script to remove double repetition for these particular words..If these character occurs replace it with single.

/[\s.'-,{2,0}]

These are character that if they comes I need to replace it with single same character.

like image 857
Aditii Avatar asked Aug 24 '11 08:08

Aditii


People also ask

Can regex replace characters?

They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.

How do I replace multiple characters in a string?

Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.

How do you replace a repeated character in a string in python?

When it is required to replicate the duplicate occurrence in a string, the keys, the 'index' method and list comprehension can be used. The list comprehension is a shorthand to iterate through the list and perform operations on it.


1 Answers

Is this the regex you're looking for?

/([\s.'-,])\1+/

Okay, now that will match it. If you're using Perl, you can replace it using the following expression:

s/([\s.'-,])\1+/$1/g

Edit: If you're using :ahem: PHP, then you would use this syntax:

$out = preg_replace('/([\s.\'-,])\1+/', '$1', $in);

The () group matches the character and the \1 means that the same thing it just matched in the parentheses occurs at least once more. In the replacement, the $1 refers to the match in first set of parentheses.

Note: this is Perl-Compatible Regular Expression (PCRE) syntax.

From the perlretut man page:

Matching repetitions

The examples in the previous section display an annoying weakness. We were only matching 3-letter words, or chunks of words of 4 letters or less. We'd like to be able to match words or, more generally, strings of any length, without writing out tedious alternatives like \w\w\w\w|\w\w\w|\w\w|\w.

This is exactly the problem the quantifier metacharacters ?, *, +, and {} were created for. They allow us to delimit the number of repeats for a portion of a regexp we consider to be a match. Quantifiers are put immediately after the character, character class, or grouping that 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 not more than "m" times.

  • a{n,} means: match at least "n" or more times

  • a{n} means: match exactly "n" times

like image 156
amphetamachine Avatar answered Sep 28 '22 02:09

amphetamachine