0

I want to capture Integer in between certain string. My Regular Expression String is re_str = "HOWMUCH?*\s*_*\s*(\s*(\d+)\s*)_*\s*USD"

target_text = " 1. HOWMUCH?__10_ USD
 1. HOWMUCH?_20_USD
 1. HOWMUCH?30__ USD
 1. HOWMUCH?__40___ USD
 1. HOWMUCH?  50 USD
 1. HOWMUCH? __60___USD"
# I need to capture all int: 10, 20, 30 ... 60

Works well in here: regex101

I have tried re.findall

m = re.findall(r'{}'.format(re_str), target_text)
m:  [('20', '20')]

also tried re.search, got the same results. python repl

Environment: Python2.7

SOLVED
I modified RegExp to HOWMUCH?*[\s]*[_]*[\s]*(\s*(\d+)\s*)[_]*[\s]*USD then it works !

stackoverYC
  • 278
  • 3
  • 13
  • [`re.findall`](https://docs.python.org/2/library/re.html#re.findall): *If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.* – Wiktor Stribiżew Apr 20 '19 at 08:16
  • I modified the question, it's a python regexp question, not the behavior on re.findall() function. – stackoverYC Apr 20 '19 at 08:34
  • Note that your [original regex](https://regex101.com/r/XpKeQt/1) also works the same, there is no issue. Just when you use 2 capturing groups you will have a list of tuples in the output. Why did you use two capturing groups? `?[_\s]*(\d+)[__\s]*USD` also works, see [demo](https://regex101.com/r/MdtCv1/1), and there will be only numbers in the list. – Wiktor Stribiżew Apr 20 '19 at 09:10

0 Answers0