如何使正则表达式匹配的一部分成为可选的? [英] How do I make part of a regex match optional?
问题描述
这是一个示例字符串:
123456#p654321
目前,我正在使用此匹配将 123456
和 654321
捕获到两个不同的组中:
Currently, I am using this match to capture 123456
and 654321
in to two different groups:
([0-9].*)#p([0-9].*)
但有时,字符串的 #p654321
部分将不存在,因此我只想捕获第一组.我试图通过将 ?
附加到第二组来使第二组可选",这是可行的,但前提是剩余字符串的末尾有 #p
.
But on occasions, the #p654321
part of the string will not be there, so I will only want to capture the first group. I tried to make the second group "optional" by appending ?
to it, which works, but only as long as there is a #p
at the end of the remaining string.
解决此问题的最佳方法是什么?
What would be the best way to solve this problem?
推荐答案
您在捕获组之外拥有 #p
,这使其成为结果的必需部分.您还错误地使用了点字符 (.
).点(在大多数 reg-ex 变体中)将匹配任何字符.改为:
You have the #p
outside of the capturing group, which makes it a required piece of the result. You are also using the dot character (.
) improperly. Dot (in most reg-ex variants) will match any character. Change it to:
([0-9]*)(?:#p([0-9]*))?
(?:)
语法是您获得非捕获组的方式.然后我们只捕获您感兴趣的数字.最后,我们将整个内容设为可选.
The (?:)
syntax is how you get a non-capturing group. We then capture just the digits that you're interested in. Finally, we make the whole thing optional.
此外,大多数 reg-ex 变体都有一个用于数字的 d
字符类.所以你可以进一步简化:
Also, most reg-ex variants have a d
character class for digits. So you could simplify even further:
(d*)(?:#p(d*))?
正如另一个人指出的那样,*
运算符可能会匹配 零 个数字.为防止出现这种情况,请改用 +
运算符:
As another person has pointed out, the *
operator could potentially match zero digits. To prevent this, use the +
operator instead:
(d+)(?:#p(d+))?
这篇关于如何使正则表达式匹配的一部分成为可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!