-1

I want to parse string which has length & precision in it eg:'(10,2)' and I need to take out length & precision.

Need output as:

_len = 10, _pre = 2

I tried below but it's not working,

>>> import re
>>> my_str = 'numeric(10,2)'
>>> m = re.match(r'\d+,\d+', my_str)
>>> m
>>> m = re.match(r'(\d+,\d+)', my_str)
>>> m
>>> m = re.match('\((+d),(+d)\)', my_str)
>>> m = re.match('\((+d),(+d)\)', my_str)
Traceback (most recent call last):
Murtuza Z
  • 5,639
  • 1
  • 28
  • 52

1 Answers1

2

re.match starts searching from the beginning of the line, that's why you are not getting any match.

Use re.search instead:

>>> m = re.search(r'(\d+),(\d+)', my_str)
>>> if m:
...     _len, _pre = map(int, m.groups())
... 
>>> _len, _pre
(10, 2)
timgeb
  • 76,762
  • 20
  • 123
  • 145