Assistance with regular expressions in Python
Posted
by
da5id
on Stack Overflow
See other posts from Stack Overflow
or by da5id
Published on 2012-10-20T22:25:01Z
Indexed on
2012/10/20
23:00 UTC
Read the original article
Hit count: 192
I am still learning REGEX, and I've run into an issue ...
I am trying to separate a string that is composed of a mixture of letters and numbers that are in decimal format:
AB0.500CD1.05EF2.29
Into something like this:
list1 = AB,CD,EF
list2 = 0.500,1.05,2.29
A complication to all this is that I also have strings that look like this:
AB1CD2EF3
Which I'd also like to separate into this:
list1 = AB,CD,EF
list2 = 1,2,3
A previous inquiry yielded the following snippet,
import re
pattern = re.compile(r'([a-zA-Z]+)([0-9]+)')
for (letters, numbers) in re.findall(pattern,cmpnd):
print numbers
print letters
This example works fine for strings of the 2nd kind, but only "finds" the leading digit in the numbers that contain decimal places in the strings of the first kind.
I've attempted an approach using the following line:
pattern = re.compile(r'([a-zA-Z]+)([0-9]+(\.[0-9]))')
But this results in an error: "ValueError: too many values to unpack"
Thanks for any and all assistance!
© Stack Overflow or respective owner