I need help creating a Regex to get numbers between parenthesis when my values are between word "PIC" and the "."
I got this records and need to be able to extract values between ()
PIC S9(02)V9(05). I need this result "02 05"
PIC S9(04). I need this result "04"
PIC S9(03). I need this result "03"
PIC S9(03)V9(03). I need this result "03 03"
PIC S9(02)V9(03). I need this result "02 03"
PIC S9(04). I need this result "04"
PIC S9(13)V9(03). I need this result "13 03"
I have try the below but it doesnt work.
s = "PIC S9(02)V9(05)."
m = re.search(r"\([0-9]+([0-9]))\", s)
print m.group(1)
You can use
re.findall()
to find all numbers within the parenthesis:where
\(
and\)
would match the literal parenthesis (needed to be escaped with a backslash because of the special meaning they have).(\d+)
is a capturing group that would match one or more digits.Assumingly, your numbers are somewhat logically connected, you might therefore come up with the following code (including the explanation):
See a demo on regex101.com as well as on ideone.com.
Hint:
If you're having list items, simple go for
\(\d+\)
.