Mor*_*Brb 5 python regex location replace
我想将第一次出现的日期或一般的正则表达式带到我的文本的开头:
示例:
"I went out on 1 sep 2012 and it was better than 15 jan 2012"
我想得到
"1 sep 2012, I went out on and it was better than 15 jan 2012"
我正在考虑替换"1 sep 2012",",1 sep 2012,"然后切割字符串,","但我不知道写什么,而不是replace_with:
line = re.sub(r'\d+\s(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}', 'replace_with', line, 1)
Run Code Online (Sandbox Code Playgroud)
任何帮助?
使用捕获组:
>>> import re
>>> s = "I went out on 1 sep 2012 and it was better than 15 jan 2012"
>>> r = re.compile('(^.*)(1 sep 2012 )(.*$)')
>>> r.sub(r'\2\1\3',s)
'1 sep 2012 I went out on and it was better than 15 jan 2012'
Run Code Online (Sandbox Code Playgroud)
括号捕获字符串的一部分:
(^.*) # Capture everything from the start of the string
(1 sep 2012 ) # Upto the part we are interested in (captured)
(.*$) # Capture everything else
Run Code Online (Sandbox Code Playgroud)
然后只需对替换`\2\1\3' 注释中的捕获组重新排序:引用捕获组需要原始字符串r'\2\1\3'.我的示例中的第二个组只是文字字符串,(1 sep 2012 )但当然这可以是任何正则表达式,例如您创建的那个(最后有一个额外\s的):
(\d+\s(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}\s)
>>> r = re.compile(r'(^.*)(\d+\s(?:aug|sep|oct|nov)\s\d{4}\s)(.*$)')
>>> r.sub(r'\2\1\3',s)
'1 sep 2012 I went out on and it was better than 15 jan 2012'
Run Code Online (Sandbox Code Playgroud)
当存在'r'或'R'前缀时,字符串中包含反斜杠后面的字符而不进行更改.