2013-01-04 67 views
3

我想帶日期的正則表達式或國內首個普遍發生,我的文字的開頭:重新排序使用字符串正則表達式

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

有幫助嗎?

+3

您使用哪種語言? – Cerbrus

+1

Python,可能。 –

+0

您沒有使用足夠的捕獲組... – 2013-01-04 08:09:25

回答

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' 

支架捕捉部分字符串:

(^.*)   # 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」前綴時,字符串中包含反斜槓後的字符而不更改。

相關問題