Regex to Replace Multiple Spaces With a Single Space

Regex to replace multiple spaces with a single space

Given that you also want to cover tabs, newlines, etc, just replace \s\s+ with ' ':

string = string.replace(/\s\s+/g, ' ');

If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:

string = string.replace(/  +/g, ' ');

Regex to replace multiple spaces to single space excluding leading spaces

You can use a regex like this:

\b\s+\b

With a space _ substitution

Working demo

Sample Image

Update for IntelliJ: seems the lookarounds aren't working on IntelliJ, so you can try this other workaround:

(\w+ )\s+

With replacement string: $1

Working demo

Of course, above regex will narrow the scenarios but you can try with that.

How do I replace multiple spaces with a single space in C#?

string sentence = "This is a sentence with multiple    spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
sentence = regex.Replace(sentence, " ");

replace multiple whitespace with single space but keep new lines in regex match (python)

You can use code below:

import re
test = "This is a test. \n Blah blah."
print(re.sub(r"(?:(?!\n)\s)+", " ",test))

Output

This is a test. 
Blah blah.

replace multiple space with single space in a column

Number of consecutive space characters can either be odd or even. You can replace two space characters with one space character, and do a similar replace again on the modified string to cover all the odd/even cases.

UPDATE abc SET title = REPLACE(REPLACE(title, '  ', ' '), '  ', ' ');

Explanation:

  • 2 spaces: First replace will convert to 1 space. Second replace will not modify further.
  • 3 spaces: First replace will convert (2+1) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.
  • 4 spaces: First replace will convert (2+2) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.

and so on...


DEMO:

mysql> select 
-> dt.test_str,
-> REPLACE(REPLACE(dt.test_str, ' ', ' '), ' ', ' ') AS modified
-> FROM
-> (SELECT 'thi s is a weird string' AS test_str) AS dt ;
+--------------------------------+--------------------------+
| test_str | modified |
+--------------------------------+--------------------------+
| thi s is a weird string | thi s is a weird string |
+--------------------------------+--------------------------+
1 row in set (0.00 sec)

How to correctly replace multiple white spaces with a single white space in PHP?

When passing u modifier, \s becomes Unicode-aware. So, a simple solution is to use

$new_str = preg_replace("/\s+/u", " ", $str);
^^

See the PHP online demo.

Regex replace multiple spaces unless preceded by . or followed by digit

(?<!\.)\s\s+(?!\d|\s)

Not a ., one space, one or more additional spaces followed by not a number or another space.

https://regex101.com/r/sfUfRI/1/

replace multiple spaces in a string with a single space

Your regex replaces any number of spaces (including zero) with a space. You should only replace two or more (after all, replacing a single space with itself is pointless):

$s = preg_replace("/ {2,}/", " ", $x);

php Replacing multiple spaces with a single space

Use preg_replace() and instead of [ \t\n\r] use \s:

$output = preg_replace('!\s+!', ' ', $input);

From Regular Expression Basic Syntax Reference:

\d, \w and \s

Shorthand character classes matching
digits, word characters (letters,
digits, and underscores), and
whitespace (spaces, tabs, and line
breaks). Can be used inside and
outside character classes.



Related Topics



Leave a reply



Submit