regex – When trying to use d+s+/d+s+ in Python, it doesnt work
regex – When trying to use d+s+/d+s+ in Python, it doesnt work
You should move the trailing whitespace matching pattern right after /
, change the +
quantifiers after s
to *
, and then you may use
import re
text = 6/6, 7 / 40, 7/ 6, 8 /97
print(re.sub(rd+s*/s*d+, number of number , text))
See the regex demo and the Regulex graph:
Pattern details
d+
– 1+ digitss*/s*
– a/
enclosed with 0 or more whitespacesd+
– 1+ digits.
NOTE: In Python 3.x re
, shorthand character classes are Unicode aware. To only match ASCII digits, [0-9]
, either use this character class, or use re.ASCII
flag (it will also affect s
though):
re.sub(rd+s*/s*d+, number of number , text, flags=re.ASCII)