Python_beginner
Python_beginner

Reputation: 99

how to use regex for finding numbers with comma after a specific string in python?

how to find numbers with comma after certain string using regex python3?

import re
word = re.search(r'sum RM (\d+) \d+(?:,\d+)?', 'sum RM 320,000.00')
print(word)


 Expected:
 RM 320,000.00

Upvotes: 1

Views: 120

Answers (3)

Allan
Allan

Reputation: 12438

If you do not only want to match numbers with , in it but if those numbers must respect the standard of adding a comma after every 3 digits for reading purpose, then use the regex:

r'sum (RM [0-9]{1,3}(:?,[0-9]{3})+(:?\.[0-9]+)?)'

Explanations:

  • [0-9]{1,3} 1 to 3 digits in the range [0-9] if you use \d you will also accept unicode digits like: 112233
  • (:?,[0-9]{3})+ comma followed by some group of 3 ascii digits repeated 1 to N times with N being an int > 1
  • (:?\.[0-9]+)? eventually some decimal part

code:

import re

#to match only numbers with comma every 3 digits
regex = r'sum (RM [0-9]{1,3}(:?,[0-9]{3})+(:?\.[0-9]+)?)'

print(re.search(regex, 'sum RM 320,000.00').group(1) if re.search(regex, 'sum RM 320,000.00') else None)
print(re.search(regex, 'sum RM ,,,').group(1) if re.search(regex, 'sum RM ,,,') else None)
print(re.search(regex, 'sum RM 3,00').group(1) if re.search(regex, 'sum RM 3,00') else None)
print(re.search(regex, 'sum RM 3123,000').group(1) if re.search(regex, 'sum RM 3123,000') else None)
print(re.search(regex, 'sum RM 31,000').group(1) if re.search(regex, 'sum RM 31,000') else None)
print(re.search(regex, 'sum RM 312,000').group(1) if re.search(regex, 'sum RM 312,000') else None)
print(re.search(regex, 'sum RM 312,00').group(1) if re.search(regex, 'sum RM 312,00') else None)

output:

RM 320,000.00
None
None
None
RM 31,000
RM 312,000
None

Upvotes: 1

Austin
Austin

Reputation: 26039

re.search returns match groups which you need to capture:

import re

word = re.search(r'sum (RM \d+(?:,\d+)(?:.\d+)?)', 'sum RM 320,000.00')
print(word.group(1))

# RM 320,000.00

Upvotes: 1

Amit Nanaware
Amit Nanaware

Reputation: 3346

try this:

import re
word = re.findall(r'sum (RM [0-9,.]*)', 'sum RM 320,000.00')
print(word)

Upvotes: 0

Related Questions