Tuesday, 6 August 2013

Python parentheses and returning only certain part of regex

Python parentheses and returning only certain part of regex

I have a list of strings that I'm looping through. I have the following
regular expression (item is the string I'm looping through at any given
moment):
regularexpression = re.compile(r'set(\d+)e', re.IGNORECASE)
number = re.search(regularexpression,item).group(1)
What I want it to do is return numbers that have the word set before them
and the letter e after them.
However, I also want it to return numbers that have set before them and x
after them. If I use the following code:
regularexpression = re.compile(r'set(\d+)(e|x)', re.IGNORECASE)
number = re.search(regularexpression,item).group(1)
Instead of returning just the number, it also returns e or x. Is there a
way to use parentheses to group my regular expression into bits without it
returning everything in the parentheses?

No comments:

Post a Comment