[英]Java Regex to match a specific string or another specific string, or not at all?
Imagine capturing the input with a regex: 想象一下使用正则表达式捕获输入:
2.1_3_4
3.2.1
3.2.1.RELEASE
3.2.1.SNAPSHOT
The numbers and the dots are easy enough to get 数字和点很容易获得
([0-9\._]+)
But how do you capture that plus "RELEASE" or "SNAPHOT" or none of those? 但是,您如何捕获该信息以及“ RELEASE”或“ SNAPHOT”,或者全都不捕获?
I played around with the or operator to no avail... 我玩了or运算符无济于事...
([0-9\._]+RELEASE||SNAPSHOT) // no worky
btw, this is a nice regex tester: http://java-regex-tester.appspot.com/ 顺便说一句,这是一个不错的正则表达式测试器: http : //java-regex-tester.appspot.com/
I think you want this: 我想你想要这个:
([0-9._]+(RELEASE|SNAPSHOT)?)
The (inside) parens form a group, and the question mark indicates the group may occur 0 or 1 times. (内部)括号组成一个组,问号指示该组可能出现0或1次。
You are doing great. 你做的很棒。 You just need to make a few changes. 您只需要进行一些更改。
First, you do not use ||
首先,您不使用||
for or, |
为或|
is used. 用来。 So RELEASE||SNAPSHOT
would convert to RELEASE|SNAPSHOT
. 因此, RELEASE||SNAPSHOT
将转换为RELEASE|SNAPSHOT
。
Since release or snapshot is not mandatory, a ?
由于版本或快照不是强制性的,因此请输入?
should be placed after it. 应该放在它后面。 So the final regex becomes 所以最终的正则表达式变成
([0-9\._]+(RELEASE|SNAPSHOT)?)
You can also use \\d
instead of 0-9
. 您也可以使用\\d
而不是0-9
。 else than this, there is no need to escape .
除此之外,没有必要逃脱.
by \\
when its present inside []
由\\
当其存在于[]
So finally, following could be the final regex 所以最后,以下可能是最终的正则表达式
([\d._]+(RELEASE|SNAPSHOT)?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.