0

내가추가 문자열

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

내가

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

로 출력을 기다리고 있었다 파이썬에서 정규 표현식을 사용하여 문자열로 날짜를 삽입하려고했다 다시하지만 난 있어요 대신이

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

또 다른 것은 내가 날짜 변수에 일부 문자열이 있다면, 그냥 잘 대체.

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

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

는 여기에서 문제가 될 수 있습니다?

답변

3

왜 어려운가요? 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=' 

또는 re로하지만, 기본적으로 같은 일을 :

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

오류를 스크립트에서, '\\1\\2'+date+'\\3''\\1\\210.12.2016\\3'로 평가했다. '\\210'은 8 진수 이스케이프로 계산되며 이는 '\x88'과 같습니다. \g<n> 시퀀스를 사용하여 문제를 해결할 수 있습니다.

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

감사 인사. 이제 작동합니다. :) –