2016-03-01 70 views
1

我想爲下面的文本創建正則表達式:如何從REGEX中的捕獲組中排除特殊字符?

date=2016-02-25 time=10:14:22+0000 

在此我們需要捕捉像下面(單拍集團)

2016-02-25 10:14:22 

我曾嘗試下面的正則表達式,但我可以不能夠實現我的O/P:

^(?!time=)\D+(\d{4}\-\d+\-\d+\s\D+\d+\:\d+\:\d+) 

是否可以創建正則表達式?請幫助我。提前致謝!

回答

0

試試這個

.*?((?:\d+-?)+).*?((?:\d+\:?)+).* 

Regex demo

說明:
.:除了換行符的任何字符sample
*:零次或多次sample
?:一旦或無sample
(…):捕獲組sample
(?: …):非捕獲組sample
\:轉義特殊字符sample
+:一個或多個sample

0

你可以只拍攝日期和時間用不同的組,並加入他們一起使用Python的字符串運算符:

import re 

text = 'date=2016-02-25 time=10:14:22+0000' 
pattern = r'^date=(\d{4}-\d{2}-\d{2}) time=(\d{2}:\d{2}:\d{2})[+-]\d{4}$' 

match = re.match(pattern, text.strip()) 
result = " ".join(match.groups()) 

print(result) 
相關問題