Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string on regex in Python [duplicate]

Tags:

python

regex

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.

like image 232
Teofrostus Avatar asked Jan 26 '26 01:01

Teofrostus


1 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']
like image 173
Avinash Raj Avatar answered Jan 27 '26 17:01

Avinash Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!