Text Extraction
Blank Lines Regex
Blank lines separate paragraphs, but too many of them make files long and messy, and some data imports fail on empty rows. This pattern uses the m flag so that the caret and dollar anchors apply to each line. Between them it allows only spaces and tabs, so a line counts as blank if it has no visible characters at all. Use it to count empty lines, to split text into paragraphs, or to detect empty rows in pasted data. To delete blank lines entirely, combine it with the newline that follows, for example ^[ \t]*\n in a global replace.
Pattern (JavaScript)
/^[ \t]*$/mStarts at the start of the line/string, then one of the characters \t, zero or more times, then the end of the line/string.
Examples
Matches
- ✓ a b
- ✓
- ✓ a b
Does not match
- ✗ a b
- ✗ single line
- ✗ x y z
Try it
4 matches (JavaScript engine)
a b a b a b single line x y z
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[ \t]*$/gm;
const text = "a\n\nb";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[ \t]*$', re.M)
text = 'a\n\nb'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[ \t]*$/m';
$text = 'a
b';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[ \\t]*$", Pattern.MULTILINE);
String text = "a\n\nb";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)^[ \t]*$`)
text := "a\n\nb"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[ \t]*$", RegexOptions.Multiline);
var text = "a\n\nb";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?m)^[ \t]*$").unwrap();
let text = "a\n\nb";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[ \t]*$/
text = 'a
b'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matches- Flag(s) m not available in this language and were left out.
Frequently asked questions
- Why does an empty string count as a match?
- The pattern allows zero characters between the anchors, so an empty input is a blank line. Check for empty input separately if that matters to you.
- How do I collapse several blank lines into one?
- Replace \n([ \t]*\n){2,} with \n\n. Runs of blank lines shrink to a single empty line between paragraphs.
- Does a line ending with a carriage return count as blank?
- Not with this exact pattern, because \r is not in the class. Add \r to the class or normalise CRLF line endings to LF first.