![](/img/trans.png)
[英]Regex python - Match newline only if it is followed by number or special character and space
[英]Regex on zero or more character but not newline nor space in Python
在Python中,使用
date_time_reg_exp = re.compile(r'\d{4}[-/:._]\d{2}[-/:._]\d{2}[\S^\n*.$]')
根據此類數據:
2019-07:27 22:04:38.635317100 -0700
2010/08/26
2019-07-27_2313hr_19sec
2019-07.27
但是,我越來越
['2010/08/26\\', '2019-07-27_', '2019-07.27\\']
它沒有接
2019-07:27 and 2019-07-27_2313hr_19sec
最后還有多余的\\\\
如何糾正?
謝謝。
字符類[\\S^\\n*.$]
匹配任何列出的字符1倍,這就是為什么它不算術2019-07:27
。
如果要匹配2019-07-27_2313hr_19sec
,則可以匹配“日期喜歡”格式,並通過匹配0+次非空格字符\\S*
遵循匹配
\d{4}[-/:._]\d{2}[-/:._]?\d{2}\S*
例如
import re
date_time_reg_exp = re.compile(r'\d{4}[-/:._]\d{2}[-/:._]?\d{2}\S*')
s = ("2019-07:27 22:04:38.635317100 -0700\n"
"2010/08/26\n"
"2019-07-27_2313hr_19sec\n"
"2019-07.27")
print(re.findall(date_time_reg_exp, s))
結果
['2019-07:27','2010/08/26','2019-07-27_2313hr_19sec','2019-07.27']
否定運算符必須是創建否定字符類的第一個字符。 要執行您嘗試的操作,請嘗試[^\\s\\n]
。 字符類無法被部分否定(如果考慮一下,這意味着什么?)-它是允許字符的枚舉,或者是以否定運算符^
開頭的禁止字符的枚舉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.