Alexey Kolosov
Alexey Kolosov

Reputation: 147

When trying to use \d+\s+/\d+\s+ in Python, it doesn't work

I need to replace text, contained a number, maybe some spaces, then "/", then maybe some spaces and a number, to ' number of number ', but no all methods work:

import re

text = '6/6, 7 / 40, 7/ 6, 8 /97'
print(re.sub(r'\d+/\d+', ' number of number ', text))  # works
print(re.sub(r'\d+\s+/\d+', ' number of number ', text)) # works
print(re.sub(r'\d+/\d+\s+', ' number of number ', text)) # not works
print(re.sub(r'\d+\s+/\d+\s+', ' number of number ', text)) # not works

Upvotes: 0

Views: 1932

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

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(r'\d+\s*/\s*\d+', ' number of number ', text))

See the regex demo and the Regulex graph:

enter image description here

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(r'\d+\s*/\s*\d+', ' number of number ', text, flags=re.ASCII)

Upvotes: 3

Related Questions