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:

enter

Pattern details

  • d+ – 1+ digits
  • s*/s* – a / enclosed with 0 or more whitespaces
  • d+ – 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)

regex – When trying to use d+s+/d+s+ in Python, it doesnt work

Leave a Reply

Your email address will not be published. Required fields are marked *