0

New to python and 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.

8 Answers 8

3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

The r raw string protection can be removed, because \s is not interpreted by Python.
What do you mean by "The advantage, if you split often in your code, is to skip the regex machine"? Using the splitting function re.compile('\s*,\s*').split is slightly faster than the map(str.strip, s.split(…)) approach, with Python 2.7 on OS X.
3

Ha, another solution w/o regexp:

x="Hello, World, Nice"
[y.strip() for y in x.split(",")]

Comments

0
>>> a = "Hello, World, Nice"
>>> a.split(", ")
['Hello', 'World', 'Nice']
>>> 

using re:

>>> import re
>>> re.split(', ',a)
['Hello', 'World', 'Nice']
>>> 

Comments

0
re.split(', ', string)

does what you want.

Comments

0

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

Comments

0

A slightly more robust solution:

>>> import re
>>> pattern = re.compile(' *, *')
>>> l = "Hello,  World , Nice"
>>> pattern.split(l)
['Hello', 'World', 'Nice']
>>> 

Comments

0

Split on ', ', with a space

re.split(', ', string)

Comments

-1

try this regex for split

>>> a = "Hello, World, Nice"
>>> a.split("[ ,\\,]")

in regex first is space and second is comma

1 Comment

That's no regex split. str.split() doesn't use regex. And if it would, it would split a "Hello World" too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.