ramius
ramius

Reputation: 211

Capturing variable-length groups of spaces

I am trying to parse strings of the form:

XXX XXXX    XXXXXX   XX           XXXXXXXXXXXXX XXX

The objective is to capture all the variable length groups of spaces in this string. How would I do this using regex?

Upvotes: 0

Views: 699

Answers (1)

Stanislav Shabalin
Stanislav Shabalin

Reputation: 19278

import re
re.findall(r'\s+', 'XXX XXXX    XXXXXX   XX           XXXXXXXXXXXXX XXX')

Which gives: [' ', ' ', ' ', ' ', ' ']

r'\s+' means capture any groups of whitespace characters (1 or more). If you need strictly spaces, replace it with r' +'.

re.findall finds all non-overlapping matches in the string.

Upvotes: 2

Related Questions