Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to remove characters after multiple white spaces

Hi all I’ve got and input strings:

list_of_strings = ["apple", "orange ca", "pear  sa", "banana    sth"]

And I want to remove everything after multiple white spaces (more than 1), so end result is:

final_list_of_strings = ["apple", "orange ca", "pear", "banana"]

I’ve tried regex:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import re

regex_expression = r"(.*\s?)(\s{2,}.*)"

for name in list_of_strings:
    regex_matching_groups = re.findall(regex_expression, name)
    if regex_matching_groups:
    name = regex_matching_groups[0][0]
    

but fails on multiple spaces … Thank you for help!

>Solution :

You can use re.sub in a list comprehension:

import re
list_of_strings = ["apple", "orange ca", "pear  sa", "banana    sth"]
list_of_strings = [re.sub(r'\s{2}.*', '', x, flags=re.S) for x in list_of_strings]
print(list_of_strings)
# -> ['apple', 'orange ca', 'pear', 'banana']

See the Python demo.

The \s{2}.* regex matches two whitespace chars and then the rest of the string (even if there are line break chars due to re.S flag).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading