Text Extraction
Multiple Consecutive Spaces Regex
Text copied from PDFs, spreadsheets and old typewriter-style documents often contains runs of extra spaces. This pattern matches a space character repeated two or more times. Pair it with a global replace and a single space as the replacement to collapse every run in one pass while leaving single spaces untouched. It deliberately looks for the space character only, not tabs or newlines, so indentation made with tabs and line structure stay intact. If you want to squeeze every kind of horizontal whitespace, switch the space to [ \t] and the same quantifier keeps working.
Examples
Matches
- ✓ a b
- ✓ trailing
- ✓ two spaces here
Does not match
- ✗ a b c
- ✗ single spaced text
- ✗ tab here
Try it
3 matches (JavaScript engine)
a b trailing two spaces here a b c single spaced text tab here
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
JavaScript (ECMAScript 2018+)
const re = / {2,}/g;
const text = "a b";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
Python 3 (re module)
import re
pattern = re.compile(r' {2,}')
text = 'a b'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
PHP (PCRE2, preg_*)
<?php
$pattern = '/ {2,}/';
$text = 'a b';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
Java 17+ (java.util.regex)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(" {2,}");
String text = "a b";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
Go 1.22+ (regexp, RE2 syntax)
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(` {2,}`)
text := "a b"
fmt.Println(re.FindAllString(text, -1))
}C#
C# / .NET (System.Text.RegularExpressions)
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@" {2,}");
var text = "a b";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
Rust (regex crate 1.9+)
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r" {2,}").unwrap();
let text = "a b";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
Ruby (Onigmo)
pattern = / {2,}/
text = 'a b'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I collapse all extra spaces to one?
- Use text.replace(/ {2,}/g, " ") in JavaScript. Each run of two or more spaces becomes exactly one space.
- Why not use \s+ instead?
- \s also matches tabs and newlines, so replacing \s+ with a space would join separate lines together. Matching only the space character keeps paragraphs and line breaks safe.
- Does this catch non-breaking spaces?
- No. A non-breaking space is a different character, U+00A0. Use [ \u00A0]{2,} if your text comes from web pages that use them.