繁体   English   中英

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

[英]Reorder string using regular expressions

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

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

任何帮助?

使用捕获组

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

括号捕获字符串的一部分:

(^.*)          # 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'前缀时,字符串中包含反斜杠后面的字符而不进行更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM