CannedSpinach CannedSpinach - 1 year ago 67
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download