Skip to main content
ubuntuask.com

Back to all posts

How to Split By Newline And Ignore Blank Lines Using Regex?

Published on
2 min read
How to Split By Newline And Ignore Blank Lines Using Regex? image

Best Tools for Regex Enthusiasts to Buy in October 2025

1 flex & bison

flex & bison

BUY & SAVE
$16.89 $29.99
Save 44%
flex & bison
2 Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance

Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance

BUY & SAVE
$24.51 $51.95
Save 53%
Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance
3 Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others

Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others

BUY & SAVE
$47.42 $49.99
Save 5%
Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others
4 Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing

Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing

BUY & SAVE
$19.24
Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing
5 Practical Usage of Regular Expressions: An Introduction to Regexes for Translators

Practical Usage of Regular Expressions: An Introduction to Regexes for Translators

BUY & SAVE
$25.00
Practical Usage of Regular Expressions: An Introduction to Regexes for Translators
6 sed & awk: UNIX Power Tools (Nutshell Handbooks)

sed & awk: UNIX Power Tools (Nutshell Handbooks)

BUY & SAVE
$31.99
sed & awk: UNIX Power Tools (Nutshell Handbooks)
7 CRAN Recipes: DPLYR, Stringr, Lubridate, and RegEx in R

CRAN Recipes: DPLYR, Stringr, Lubridate, and RegEx in R

BUY & SAVE
$59.46
CRAN Recipes: DPLYR, Stringr, Lubridate, and RegEx in R
+
ONE MORE?

To split by newline and ignore blank lines using regex, you can use the following pattern: /\S+/g. This regex pattern matches one or more non-whitespace characters, effectively splitting the input string by newline characters and ignoring blank lines. For example, if you have the following input string:

Line 1 Line 2

Line 3

Line 4

You can use the regex pattern /\S+/g to split it into an array of non-blank lines:

["Line 1", "Line 2", "Line 3", "Line 4"]

How to remove empty lines from a string using regex?

In Python, you can remove empty lines from a string using the re.sub() function from the re module.

Here's an example code snippet to remove empty lines from a string using regex:

import re

Sample string with empty lines

text = 'Hello\n\nWorld\n\n\nHow are you?'

Remove empty lines using regex

clean_text = re.sub(r'\n\s*\n', '\n', text)

print(clean_text)

Output:

Hello World How are you?

In the regex pattern r'\n\s*\n',

  • \n matches a newline character,
  • \s* matches zero or more whitespace characters,
  • the overall pattern matches one or more whitespace characters in between two newline characters.

The re.sub() function replaces this pattern with just a single newline character, effectively removing the empty lines from the string.

What is the equivalent of new line in regex?

In regex, the equivalent of a new line is represented by the special character \n or [\r\n].

How to split a text file by newline using regex in Python?

You can split a text file by newline using regex in Python by using the re.split() function from the re module. Here's an example code snippet to split a text file by newline using regex:

import re

Read the text file

with open('file.txt', 'r') as file: text = file.read()

Split the text file by newline using regex

lines = re.split(r'\n', text)

Print the lines

for line in lines: print(line)

In this code snippet, we first read the text file using the open() function and then store the contents of the file in the text variable. We then split the text file by newline using the regex pattern \n with the re.split() function. Finally, we loop through each line in the lines list and print them.

Make sure to replace 'file.txt' with the path to your text file that you want to split by newline.