I am trying to validate a version number string. something like #.#.#.#

can you please tell me if the below is correct


how do i restrict this to just 4 decimals?

2 Answers 2


The regex you provided does not give information regarding number of decimal places. You have to specify that. You have specify pattern to have 4 decimals and each number can be at max 2 digits.

// First, instantiate a new Pattern object "MyPattern"
Pattern MyPattern = Pattern.compile('[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}');

// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher('');

// You can use the system static method assert to verify the match

There are plenty of Regex testers/fiddles out there, e.g. http://refiddle.com/

^\d\.\d\.\d\.\d$ expresses "Start string, digit, literal period, digit, period, digit, period, digit, end string"

Edit: Better yet, ^(\d\.){3}\d$ "start, 3 of (digit followed by period), and a digit.

Edit the second: assuming you're looking at IP addresses or anything else with a fixed number of digits between dots, up to 3 digits are permitted between periods: ^(\d{1,3}\.){3}\d{1,3}$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.