3

I'm new to java and I'm trying to validate user input using a regex.

I need to validate two patterns :

  • version
  • version-1.xy when x & y are digits

below is my pattern but somehow it is not working..

String versionRegex  = "^version-1.[1-9][1-9]|version";

Thanks for your help guys.

3
  • Show us examples where it is not working Commented Jun 14, 2016 at 6:08
  • You need to escape the . as \.. Everything else seems correct Commented Jun 14, 2016 at 6:08
  • 1
    How do you know that "it's not working" ? What input are you giving, what is the outcome, and what did you expect the outcome to be? Commented Jun 14, 2016 at 6:12

2 Answers 2

2

This will support version / version-1.xy / VERSION / VERSION-1.xy

String versionRegex  = "(?i)^version(-1\\.[1-9][1-9])?";
Sign up to request clarification or add additional context in comments.

Comments

1
String versionRegex  = "(?i)^version(-1\\.[1-9][1-9])?";

. needs to be escaped in java with \\, and (?i) to support case insensitive matching.

3 Comments

I'd suggest ^version(-1\\.[1-9][1-9])?
thanks!, in case I wanna support capital letters ? what should I add.. eg. VERSION-1.xy or VERSION
@jawmar You need add (?i) in front of the regexp and it will becoem case insensitive

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.