How to split a string on regex in Python

Question:

How do you incorporate a regular expression into the Python string.split method? Here is some sample code:

ip = '192.168.0.1:8080'
tokens = ip.split('[.|:]')
print tokens

This for some reason generates ['192.168.0.1:8080']. Can someone point out what I’m missing? I’ve tried escaping characters and using double quotes, but nothing seems to change anything.

Asked By: Teofrostus

||

Answers:

You need to use re.split if you want to split a string according to a regex pattern.

tokens = re.split(r'[.:]', ip)

Inside a character class | matches a literal | symbol and note that [.:] matches a dot or colon (| won’t do the orring here).

So you need to remove | from the character class or otherwise it would do splitting according to the pipe character also.

or

Use string.split along with list_comprehension.

>>> ip = '192.168.0.1:8080'
>>> [j for i in ip.split(':') for j in i.split('.')]
['192', '168', '0', '1', '8080']
Answered By: Avinash Raj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.