Michael T
Michael T

Reputation: 1965

Python Regular Expressions re.findall -- split a string into two

I have strings like this:

"C BOS - Traded from Royal Disappointments"

And I want to split them into everything before the dash, and everything after. So simply in two variables, new1 and new2, I want the above to be:

new1 = "C BOS"

new2 = "Traded from Royal Disappointments"

I'm new to re so I can't figure out how to get the findall to work. Here's what I attempted:

import re

myval = "C BOS  - Traded from Royal Disappointments"

new1 = re.findall( r'*\s\s\-',  myval )
new2 = re.findall( r'-\s*', myval)

I know it's probably not close but it's not clear to me how to make the expression.

Upvotes: 2

Views: 5515

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

Use re.split:

>>> import re
>>> s = "C BOS  - Traded from Royal Disappointments"
>>> re.split(r'\s*-\s*', s)
['C BOS', 'Traded from Royal Disappointments']

Assigning the result to variables:

>>> new1, new2 = re.split(r'\s*-\s*', s)
>>> new1
'C BOS'
>>> new2
'Traded from Royal Disappointments'

A non-regex version, but requires two passes:

>>> map(str.strip, s.split('-'))
['C BOS', 'Traded from Royal Disappointments']

If the string contains more than 1 - and you still want to split only once, then pass the split count to re.split:

>>> s = "C BOS  - Traded from Royal Disappointments  -  foobar"
>>> re.split(r'\s*-\s*', s, 1)
['C BOS', 'Traded from Royal Disappointments  -  foobar']

Upvotes: 3

Derek Litz
Derek Litz

Reputation: 10897

I'd do this without re, as long as your string example holds true.

Things to watch out for: more then one '-' or no '-'

You may want to handle the possible exception for the split and assignment.

>>> example = "C BOS  - Traded from Royal Disappointments"
>>> before, after = example.split('-')
>>> before = before.strip()
>>> after = after.strip()
>>> print before
C BOS
>>> print after
Traded from Royal Disappointments
>>>

Upvotes: 3

Related Questions