首页 > 解决方案 > 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.

标签: pythonregex

解决方案


字符类[\S^\n*.$]匹配任何列出的 1 次,这就是它不匹配 math 的原因2019-07:27

如果你想匹配2019-07-27_2313hr_19sec你可以匹配“日期喜欢”格式并通过匹配0+次非空白字符来跟随匹配\S*

\d{4}[-/:._]\d{2}[-/:._]?\d{2}\S*

正则表达式演示| Python 演示

例如

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']


推荐阅读