1

Here is my string

string = '03/25/93 Total time of visit (in minutes)'

I want to match '03/25/93' and replace it with '03/25/1993'. Currently I'm trying this

re.sub(r'(\d?\d/\d?\d/)(\d\d)', r'\119\2', string)

But apparently the '19' between '\1' and '\2' causes some errors. Is there a way to modify this method?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Danni Peng
  • 75
  • 5

1 Answers1

1

In that case you need to use the syntax \g<group>

Code

import re

string = '03/25/93 Total time of visit (in minutes)'
res = re.sub(r'(\d?\d/\d?\d/)(\d\d)', r'\g<1>19\2', string)

print(res)

Output

'03/25/1993 Total time of visit (in minutes)'

Taken from the docs

In string-type repl arguments, in addition to the character escapes and backreferences described above, \g will use the substring matched by the group named name, as defined by the (?P...) syntax. \g uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference \g<0> substitutes in the entire substring matched by the RE

Take a look at the official documentation of re.sub for better understanding

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
  • Hi, can you please explain more on the part '\g<1>'? – Danni Peng Jan 15 '19 at 03:25
  • Its just the way to desambiguate cases such as the one you have. Take a look at the docs for a better and in-depth explanation. – JoshuaCS Jan 15 '19 at 03:27
  • @DanniPeng I added a little extract from the documentation of `re`. Please, if my answer worked for you, mark it as **accepted** (green checkmark) and upvote it (if you can upvote). Thnx in advance! – JoshuaCS Jan 15 '19 at 03:31