Reputation: 3097
I'm trying to write a Regex for strings -
c190_12_1-10
c129
abc_1-90
to separate to -
['c190_12_', '1', '10']
['c', '129']
['abc_', '1', '90']
So far I've came up with (\D+)(\d+)-?(\d+)?
But, it doesn't work for all combinations. What I am missing here?
Upvotes: 1
Views: 77
Reputation: 89639
You can use this:
items = ['c190_12_1-10', 'c129', 'abc_1-90']
reg = re.compile(r'^(.+?)(\d+)(?:-(\d+))?$')
for item in items:
m = reg.match(item)
print m.groups()
Upvotes: 1
Reputation: 73044
Not sure what exactly you do and don't want to match, but this might work for you:
(?:(\w+)(\d+)-|([a-z]+))(\d+)$
The secret here was working backwards form the end, where it always seems to be the same condition. Then using the conditionals and the non-capture group, you end up with the result you've shown.
Upvotes: 0