CannedSpinach CannedSpinach - 2 months ago 8x
Python Question

Trying to capture a particular pattern with regular expressions (Python 3.4)

The pattern I'm trying to capture is along the lines of:

Coin: gp (3840)

"gp" can be replaced with "cp", "sp", or "pp", and there can be any string of digits inside the parentheses. The pattern I came up with is:

"Coin: 'cp|sp|gp|pp' \(\d+\)"

However, this is my result when I try to get a match:

>>> print(re.match("Coin: 'cp|sp|gp|pp' \(\d+\)", "Coin: gp (3840)"))

Which part of the pattern am I getting wrong?


You have pattern matching different options wrong: 'cp|sp|gp|pp' It's trying to match the literal character '.

Your regex should really be something like:

>>> re.match("Coin: (?:cp|sp|gp|pp) \(\d+\)", "Coin: gp (3840)")

The expression (?:cp|sp|gp|pp) creates a non-capturing group of your options.