New to python and开发者_StackOverflow I'm having trouble getting the function i want out of regex. Basically i have a string that looks like "Hello, World, Nice"
and i need to convert it into a list with the delimiter being ,
. End result should look like ['Hello', 'World', 'Nice']
re.split(',', string)
Basically the result i get is
['Hello', ' World', ' Nice']
.
I know a solution via a different method but i'd like to uses regex.
help much appreciated.
Assuming, that the whitespace can be arbitrary, there are two solutions, that come to mind:
re.split(r'\s*,\s*', string)
# ^- zero or more whitespace incl. tabs and newlines
# the r'' syntax preserves the backslash from being interpreted
# as escape sequence
and
map(str.strip, string.split(','))
# ^- apply the 'strip' function (~ 'trim' in other languages) to all matches
I'd go with the later. The advantage, if you split often in your code, is to skip the regex machine (although it won't sum up, until you split really often).
Ha, another solution w/o regexp:
x="Hello, World, Nice"
[y.strip() for y in x.split(",")]
>>> a = "Hello, World, Nice"
>>> a.split(", ")
['Hello', 'World', 'Nice']
>>>
using re:
>>> import re
>>> re.split(', ',a)
['Hello', 'World', 'Nice']
>>>
re.split(', ', string)
does what you want.
If you don't have specific advanced requirement, there is really no need for re module.
>>> "Hello, World, Nice".split(",")
['Hello', ' World', ' Nice']
>>> map( str.strip, "Hello, World, Nice".split(",") )
['Hello', 'World', 'Nice']
if you really insist on re.
>>> re.split('\s*,\s*', "Hello, World, Nice" )
['Hello', 'World', 'Nice']
A slightly more robust solution:
>>> import re
>>> pattern = re.compile(' *, *')
>>> l = "Hello, World , Nice"
>>> pattern.split(l)
['Hello', 'World', 'Nice']
>>>
Split on ', '
, with a space
re.split(', ', string)
try this regex for split
>>> a = "Hello, World, Nice"
>>> a.split("[ ,\\,]")
in regex first is space and second is comma
精彩评论