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.
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']
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.
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']