Regex Punctuation Split [Python]

Regex Punctuation Split [Python]

Can anyone help me a bit with regexs? I currently have this: re.split(" +", line.rstrip()), which separates by spaces.

How could I expand this to cover punctuation, too?

4 Answers

The official Python documentation has a good example for this one. It will split on all non-alphanumeric characters (whitespace and punctuation). Literally \W is the character class for all Non-Word characters. Note: the underscore "_" is considered a "word" character and will not be part of the split here.

re.split('\W+', 'Words, words, words.')

See for more examples, search page for "re.split"

2

Using string.punctuation and character class:

>>> from string import punctuation
>>> r = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
>>> r.split('dss!dfs^  #$% jjj^')
['dss', 'dfs', 'jjj', '']
1
import re
st='one two,three; four-five,    six'

print re.split(r'\s+|[,;.-]\s*', st)
# ['one', 'two', 'three', 'four', 'five', 'six']
2

When you consider using a regex to split with any punctuation, you should bear in mind that the \W pattern does not match an underscore (which is a punctuation char, too).

Thus, you can use

import re
tokens = re.split(r'[\W_]+', text)

where [\W_] matches any Unicode non-alphanumeric chars.

Since re.split might return empty items when the match appears at the start or end of string, it is better to use a positive logic and use

import re
tokens = re.findall(r'[^\W_]+', text)

where [^\W_] matches any Unicode alphanumeric chars.

See the Python demo:

import re
text = "!Hello, world!"
print( re.split(r'[\W_]+', text) )
# => ['', 'Hello', 'world', '']
print( re.findall(r'[^\W_]+', text) )
# => ['Hello', 'world']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.