简体   繁体   English

使用Python正则表达式在表达式中可选地匹配?

[英]Matching optionally in expression with Python regex?

I have written the following regex to match human-readable time at the command-line: 我编写了以下正则表达式来匹配命令行中的人类可读时间:

^(?:(?:(?:(\d+)d\s*)?(\d+)h\s*)?(\d+)m\s*)?(\d+)s$

Using non-capturing strings, this regex matches "human-readable" time equally-well in the following formats: 使用非捕获字符串,此正则表达式在以下格式中同样匹配“人类可读”时间:

1d 2h 3m 4s
1h 2m 3s
1m 2s
1s

...and... ...和...

1d2h3m4s
1h2m3s
1m2s
1s

In this regex, if I include a minutes value, I also have to include a seconds value. 在这个正则表达式中,如果我包含minutes值,我还必须包含seconds值。 Ie, I can't simply provide 15m or 1d3m , I have to provide 15m0s or 1d0h3m0s . 即,我不能简单地提供15m1d3m ,我必须提供15m0s1d0h3m0s

Is it possible to extend a regex to match these latter two use cases? 是否有可能扩展正则表达式以匹配后两个用例? How? 怎么样? Please note: I'm not necessarily looking for a drop-in solution, but a pointer in the right direction would be greatly appreciated. 请注意:我不一定要寻找插入式解决方案,但是非常感谢正确方向的指针。

Update 更新

Just a brief update that I made awhile back - this is for regex in Python. 只是我做了一段时间的简短更新 - 这是Python中的正则表达式。

You can use this pattern: 您可以使用此模式:

\A(?=\S)(?:\d+d)?(?:\h*\d+h)?(?:\h*\d+m)?(?:\h*\d+s)?\z

The approach is to make all element optional. 方法是使所有元素都是可选的。 The lookahead at the begining ensures that there is at least a character that is not a space. 开头的前瞻确保至少有一个不是空格的角色。 (in other words, it ensures that there is at least one element) (换句话说,它确保至少有一个元素)

Rather that maintaining that regular expression and trying to tweak it I would suggest greatly simplifying your regex to this: 相反,保持正则表达式并尝试调整它我会建议大大简化你的正则表达式:

/ *(\d+)([dhms])/gm

RegEx Demo RegEx演示

As you can see it matches all your current and proposed strings. 如您所见,它匹配您当前和建议的所有字符串。 You can then post-process both captured groups in your code. 然后,您可以在代码中对两个捕获的组进行后处理。

your seconds files is not optional.there is no ? 你的秒文件不是可选的。没有? after it.so all fields not containg s will fail. 在它之后。所有不包含s的字段都会失败。

See demo. 见演示。

http://regex101.com/r/iX5xR2/28 http://regex101.com/r/iX5xR2/28

I have applied question mark. 我申请了问号。

You can use nested groups: 您可以使用嵌套组:

/^(?:(?:(?:(\d+)d\s*)?(\d+)h\s*)?(\d+)m\s*)?(\d+)s$/g

The value for d , h , m and s are in groups 1, 2, 3 and 4 respectively. dhms的值分别为1,2,3和4组。

Here is a regex demo ! 这是一个正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM