4

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?

1
  • How do you want the split to be? Should it return only Foo? only the number? both? Commented Jan 28, 2010 at 21:04

5 Answers 5

6

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')
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

Keeping it simple:

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

1 Comment

simple... and allowing for digits in the "arbitrary string" :-p
0

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.

3 Comments

Great idea, but won't work, at least in Python. It ignores lookarounds in regexes that do not match any characters.
...seriously? I wonder what the rationale for that behaviour is.
Hmm, it seems to ignore them for split() but it does work for search(). Strange.
0
>>> 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.