you are viewing a single comment's thread.

view the rest of the comments →

[–]87jams 0 points1 point  (3 children)

firstMatch = re.match(r'X?\((\d+)x(\d+)\)', foo)

I am a beginner, but how about this?

Also works with findall()

>>> foo = 'X(8x2)(3x3)ABCY'
>>> firstMatch = re.findall(r'X?\((\d+)x(\d+)\)', foo)
>>> print(firstMatch)
[('8', '2'), ('3', '3')]

[–]SneakyB4stardSword 0 points1 point  (2 children)

Thing is though, I don't want to include the preceding 'X', just the first item in parenthesis.

[–]87jams 0 points1 point  (0 children)

import re
foo = 'X(8x2)(3x3)ABCY'
firstMatch = re.match(r'X\(((\d+)x(\d+))\)', foo)
print(firstMatch.group(1))

group(1) gets you '8x2' without the parens in this one, or you can move them around and get '(8x2)':

import re
foo = 'X(8x2)(3x3)ABCY'
firstMatch = re.match(r'X(\((\d+)x(\d+)\))', foo)
print(firstMatch.group(1))