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.”
Requirements
If you can formulize what you want as:
1.5or1.5.6or1.5.6.7or1.5.6.7.8etc. is valid version number by default (numbers can be one or more digits like12.60.70).1.5can be followed by a dot (.) only if it is in the format1.5.6.1.5never preceded by a dot (.) like.1.5.Solution
Then, I can suggest you this regex:
See it in action, includes all positive samples and excludes all negative samples you provided.
How to use
First capture group will be your version number. Sample code:
Note: This will work only with Java 7+, because look-aheads doesn’t supported by older versions.