I am trying to validate a version number string. something like #.#.#.#
can you please tell me if the below is correct
^[0-9]{1,2}(.[0-9]{1,2})?
how do i restrict this to just 4 decimals?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityThe 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('10.1.2.22');
// You can use the system static method assert to verify the match
System.assert(MyMatcher.matches());
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}$