1

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?

I played around with the or operator to no avail...

([0-9\._]+RELEASE||SNAPSHOT)  // no worky

btw, this is a nice regex tester: http://java-regex-tester.appspot.com/

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Probably (?:RELEASE|SNAPSHOT)? to avoid the inner match group.
@pickypg Yours would avoid matching the inner match group, but I think the op wants that group. Perhaps the op could clarify.
Agreed, but based on his original attempt and 'plus "RELEASE" or "SNAPHOT"', I suspect he wants to basically ensure that it passes and get the whole thing. (Though it's worth noting that ...._RELEASE would match, which he might not expect)
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.
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. 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)?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.