0

I was trying to insert a date into a string, using regular expression in python

link = 'branch=;deps=;date=;rev=;days=1;user='
date = "10.12.2016"
re.sub(r'(.*)(date=[^;]*)(.*)','\\1\\2'+date+'\\3',link)

I was expecting the output to be

'branch=;deps=;date=10.12.2016;rev=;days=1;user='

but I got this instead,

'branch=;deps=;**\x88.12.2016**;rev=;days=1;user='

Another thing if I have some character string in the date variable, it is replacing just fine.

date="hello"
re.sub(r'(.*)(date=[^;]*)(.*)','\\1\\2'+date+'\\3',link)

gives,

'branch=;deps=;**date=hello**;rev=;days=1;user='

What could be the problem here?

Gopikrishnan R
  • 11
  • 1
  • 1
  • 4

1 Answers1

3

Why make it difficult? Skip re:

>>> link = 'branch=;deps=;date=;rev=;days=1;user='
>>> date = "10.12.2016"
>>> link = link.replace('date=','date='+date)
>>> link
'branch=;deps=;date=10.12.2016;rev=;days=1;user='

Or with re, but basically the same thing:

>>> re.sub(r'date=','date='+date,link)
'branch=;deps=;date=10.12.2016;rev=;days=1;user='

The error in your script, was '\\1\\2'+date+'\\3' evaluates to '\\1\\210.12.2016\\3'. '\\210' evaluates as an octal escape, which is equivalent to '\x88'. You can fix that by using the \g<n> sequence:

>>> re.sub(r'(.*)(date=[^;]*)(.*)','\\1\\g<2>'+date+'\\3',link)
'branch=;deps=;date=10.12.2016;rev=;days=1;user='
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251