Reputation: 537
For each string input I need to match only a 2-digit (or many) (that need to be separated by space or by comma+space (not comma+digit).
In the case below you see the result of some examples. I don't want my regex to catch the 2 first examples.
def cleanse_no(string):
regex = r"(?:^|\s)(\d{2})\b"
string = str(string).strip(" .,€")
list_digits = re.findall(regex, string)
digits = ", ".join(list_digits)
return digits
test_digits = ["€ 22.22", ". 23,600.90", "25 45 61", "22, 232, 36, 02,", "1, 23, 456"]
for test_dgt in test_digits:
print(test_dgt,"-------find_no--------->",cleanse_no(test_dgt))
I get these results:
€ 22.22 -------find_no---------> 22 . 23,600.90 -------find_no---------> 23 25 45 61 -------find_no---------> 25, 45, 61 22, 232, 36, 02, -------find_no---------> 22, 36, 02 1, 23, 456 -------find_no---------> 23
Any ideas ?
Upvotes: 1
Views: 55
Reputation: 626871
You may use
def cleanse_no(s):
regex = r"(?<!\S)\d{2}(?=,?(?:\s|$))"
return ", ".join(re.findall(regex, s))
See the Python demo and the regex demo.
Pattern details
(?<!\S)
- a whitespace left boundary\d{2}
- two digits(?=,?(?:\s|$))
- immediately to the right of the current position, there must be an optional comma followed with either a whitespace or end of string.Upvotes: 2