Can any body give me some regular expression patterns which will extract the version numbers(1.5) from some sample strings like
"jdk1.5","jdk-v1.5","jdk-V1.5","jdk V1.5","jdkv1.5","jdk version1.5","1.5.6","v1.5","V1.5","version 1.5","Version 1.5","14.5.4","1.5.4","14.5.4""jdj14.5"
I want to store the regex patterns in a string array and will check these patterns with the above strings. If there is a match with any of the stored patterns, then the output should be the version number 1.5. I want to extract the version numbers (1.5) only from the above strings.
valid string formats : "jdk1.5","jdk-v1.5","jdk-V1.5","jdk V1.3","jdkv1.4","jdk version1.5","1.5.6","v1.5","V1.5","version 1.5","Version 1.5","14.5.4","1.5.4","14.5.4""jdj14.5","14.52.3.42"
Invalid String formats : "jdk1..2","jdk.1.2.",".1.2."
Try with:
^(jdk[- ]?)?([vV](ersion)? ?)?\d\.\d(\.\d)?$
You can ommit [vV]
with v
if you set CASE_INSENSITIVE
flag.
Try this regular expression:
(?<=([\\w\\s]))(\\d)\\.(\\d)(.\\d)?
This will match all your valid formats , and not match any invalid format.
See it working here : http://regexr.com?31ubc
I would be grateful if someone can assist me in making it better, and also on how to make it match 1.5.6
If you can formulize what you want as:
1.5
or 1.5.6
or 1.5.6.7
or 1.5.6.7.8
etc. is valid version number by default (numbers can be one or more digits like 12.60.70
). 1.5
can be followed by a dot ( .
) only if it is in the format 1.5.6
. 1.5
never preceded by a dot ( .
) like .1.5
. Then, I can suggest you this regex:
(?!\.)(\d+(\.\d+)+)(?![\d\.])
See it in action , includes all positive samples and excludes all negative samples you provided.
First capture group will be your version number. Sample code:
Pattern pattern = Pattern.compile("(?!\\.)(\\d+(\\.\\d+)+)(?![\\d\\.])");
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound)
{
String version = matcher.group(1);
System.out.println("Version number: " + version);
}
else
{
System.out.println("No match for the input!");
}
Note: This will work only with Java 7+, because look-aheads doesn't supported by older versions.
(?<![\\d.])(\\d+[.])+(\\d+)(?![\\d.])
Check this...
It will match the string of the form
number.number.number.number
not followed or preceded by a dot
对于1.5.6这样的字符串,您可以使用: [0-9](.[0-9])+
http://regexr.com?31ubo
Use the regex:
(^([jJ][dD][kK])([\s]*[vV]*\d+.\d+(.\d+)*))$
This would make your day :)
If you want to catch more than two numbers like "1.5.6" or "1.5.0" you can use
[0-9](.[0-9])+
Or if you only want the first two digits [0-9].[0-9]
, but this will not work on strings like 1.5.0.0, it will catch 1.5 and 0.0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.