python - Regex on zero or more character but not newline nor space in Python
问题描述
In Python, this is used
date_time_reg_exp = re.compile(r'\d{4}[-/:._]\d{2}[-/:._]\d{2}[\S^\n*.$]')
on such data:
2019-07:27 22:04:38.635317100 -0700
2010/08/26
2019-07-27_2313hr_19sec
2019-07.27
however, I am getting
['2010/08/26\\', '2019-07-27_', '2019-07.27\\']
it is not picking up
2019-07:27 and 2019-07-27_2313hr_19sec
and there is extra \\
at the end
How can this is corrected?
Thank you.
解决方案
字符类[\S^\n*.$]
匹配任何列出的 1 次,这就是它不匹配 math 的原因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']
推荐阅读
- three.js - 将 Autodesk forge 与 mapbox 一起使用时的问题
- java - 即使我在前端输入输入,也会将 NULL 更新传递给 MYSQL;使固定 ?(Spring MVC,Java)
- python - 重新组合 Python 列表项/组织给定数据
- google-directory-api - 使用 users.photos.get 获取照片
- ios - 您如何为 iOS 调用本机拨号程序以拨打 911
- c++ - 为什么我的 QSortFilterProxyModel 在与 QStandardItemModel 一起使用时会向我的 TreeView 添加额外的空白行?
- sql - 返回 SQL Server 中重复计数的 4x4 命名列和行
- python - 每当我发送 brew-coffee 请求时,我的 TeaMaster9001 都会以代码 418 进行响应
- c# - 如何更改 TokenEndpointPath
- python - 如何在数据框中将 -0.0 值替换为 0.0 值?