Ryflex
Ryflex

Reputation: 5779

Need help changing a regex to make it split correctly

My code below is going wrong at the moment, the top three examples work fine but the last one isn't working as it should.

I've commented to show what the output is and what it should be.

import re

print(filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '6 Name 100/100')))
#['6', 'Name' '100/100']
## Correct

print(filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '6 Name Name-Name 1/1001')))
#['6', 'Name Name-Name', '1/1001']
## Correct

print(filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '6 Name Name`Name 1240/1422')))
#['6', 'Name Name`Name', '1240/1422']
## Correct

print(filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '6 Name Name`Name None')))
#['6', 'Name Name`Name None']
## Wrong, It should be returning:
#['6', 'Name Name`Name', 'None']

Any ideas are welcome.

Thanks - Hyflex

Upvotes: 0

Views: 56

Answers (2)

Logan Murphy
Logan Murphy

Reputation: 6230

so * is a greedy quantifier and will keep going until it can go no more. So you need to add a ? after it

(\d+) (\w.*?) (\d.*)

Also there are no \d to prefix your .* for the last group.

Upvotes: 1

Jerry
Jerry

Reputation: 71558

It seems you're trying to get a list and considering space as separators, you seem to want to get the first and last elements, then all that remains goes to the 2nd element of that result list.

If that's so, you can use match instead:

>>> print re.match(r'^(\S+) (.*?) (\S+)$', '6 Name Name`Name None').groups()
('6', 'Name Name`Name', 'None')

regex101 demo

\S matches every character that is not a space and ^ and $ match the beginning and end of line respectively.

Upvotes: 4

Related Questions