使用正则表达式重新排序字符串

时间:2013-01-04 08:04:23

标签: python regex replace location

我想将第一次出现的日期或一般的正则表达式带到我的文本的开头:

实施例: "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)

任何帮助?

1 个答案:

答案 0 :(得分:7)

使用capture groups

>>> 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'

Brackets捕获字符串的一部分:

(^.*)          # Capture everything from the start of the string
(1 sep 2012 )  # Upto the part we are interested in (captured)
(.*$)          # Capture everything else

然后只需对替换`\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'

来自docs.python.org

  

当存在'r'或'R'前缀时,字符串中包含反斜杠后面的字符不会发生变化。