簡體   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