user131091
user131091

Reputation: 207

Python regex split, integer of arbitrary length

I'm trying to do a simple regex split in Python. The string is in the form of FooX where Foo is some string and X is an arbitrary integer. I have a feeling this should be really simple, but I can't quite get it to work.

On that note, can anyone recommend some good Regex reading materials?

Upvotes: 4

Views: 2329

Answers (5)

MikeyB
MikeyB

Reputation: 3360

Keeping it simple:

>>> import re
>>> a = "Foo1String12345"
>>> re.split(r'(\d+)$', a)[0:2]
['Foo1String', '12345']

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304503

>>> import re
>>> s="gnibbler1234"
>>> re.findall(r'(\D+)(\d+)',s)[0]
('gnibbler', '1234')

In the regex, \D means anything that is not a digit, so \D+ matches one or more things that are not digits.

Likewise \d means anything that is a digit, so \d+ matches one or more digits

Upvotes: 0

Max Shawabkeh
Max Shawabkeh

Reputation: 38683

You can't use split() since that has to consume some characters, but you can use normal matching to do it.

>>> import re
>>> r = re.compile(r'(\D+)(\d+)')
>>> r.match('abc444').groups()
('abc', '444')

Upvotes: 6

AJ.
AJ.

Reputation: 28204

Using groups:

import re

m=re.match('^(?P<first>[A-Za-z]+)(?P<second>[0-9]+)$',"Foo9")
print m.group('first')
print m.group('second')

Using search:

import re

s='Foo9'
m=re.search('(?<=\D)(?=\d)',s)
first=s[:m.start()]
second=s[m.end():]

print first, second

Upvotes: 1

Anon.
Anon.

Reputation: 60063

Assuming you want to split between the "Foo" and the number, you'd want something like:

r/(?<=\D)(?=\d)/

Which will match at a point between a nondigit and a digit, without consuming any characters in the split.

Upvotes: 0

Related Questions