我在正则表达式模式匹配方面不强,但需要一些帮助。我正在尝试使用Regex.Match匹配此模式:
我检查了站点并找到了一些帮助,但并不是我所寻找的100%(没有捕获3m和7m或任何具有此类值的值),所有其他值都匹配。
根据我的发现,我将这种模式组合在一起:
(m\s+)?(?:[1-5]?\ds)
感谢您的帮助。
答案 0 :(得分:2)
此模式适用于所有示例:
\b[0-5]?\d(?:m(?:\h[0-5]?\ds)?|s)\b
演示:https://regex101.com/r/FdmNII/3
故障:
\b # Word boundary.
[0-5]?\d # A number between 00 and 59.
(?: # Start of 1st non-capturing group.
m # Matches the character `m` literally.
(?: # Start of 2nd non-capturing group.
\h # Any horizontal whitespace.
[0-5]?\d # A number between 00 and 59.
s # Matches the character `s` literally.
) # End of 2nd non-capturing group.
? # Indicates that the previous group is optional.
| # Alternation (OR).
s # Matches the character `s` literally.
) # End of 1st non-capturing group.
\b # Word boundary.
如果您还想匹配01m01s
之类的东西,则可以将空白设为可选(即\h?
代替\h
)。