thefragileomen
thefragileomen

Reputation: 1547

splitting string in Python (2.7)

I have a string such as the one below:

26   (passengers:22  crew:4)

or

32   (passengers:?  crew: ?)

. What I'm looking to do is split up the code so that just the numbers representing the number of passengers and crew are extracted. If it's a question mark, I'd look for it to be replaced by a "".

I'm aware I can use string.replace("?", "") to replace the ? however how do I go about extracting the numeric characters for crew or passengers respectively? The numbers may vary from two digits to three so I can't slice the last few characters off the string or at a specific interval.

Thanks in advance

Upvotes: 1

Views: 139

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122292

A regular expression to match those would be:

r'\(\s*passengers:\s*(\d{1,3}|\?)\s+ crew:\s*(\d{1,3}|\?)\s*\)'

with some extra whitespace tolerance thrown in.

Results:

>>> import re
>>> numbers = re.compile(r'\(\s*passengers:\s*(\d{1,3}|\?)\s+ crew:\s*(\d{1,3}|\?)\s*\)')
>>> numbers.search('26   (passengers:22  crew:4)').groups()
('22', '4')
>>> numbers.search('32   (passengers:?  crew: ?)').groups()
('?', '?')

Upvotes: 5

Related Questions