I am learning the ropes with regular expression in Python. I have the code below:
import re
test = '"(Z101+Z102+Z1034+Z104)/4"'
regex = re.compile(r"[\(\+]([XYZ]\d\d\d)[\)\+]")
regex.findall(test)
It returns:
['Z101', 'Z104']
However, when I change 'Z101' to 'YZ101':
import re
test = '"(YZ101+Z102+Z1034+Z104)/4"'
regex = re.compile(r"[\(\+]([XYZ]\d\d\d)[\)\+]")
regex.findall(test)
It returns:
['Z102', 'Z104']
The purpose is to extract strings containing X
, Y
or Z
following by any set of three digits. Therefore, the desired output for the first code would be:
['Z101', 'Z102', 'Z104']
How to fix the compile and get the correct output?