简体   繁体   English

如何为以下情况编写正则表达式模式

[英]how to write the regular expression patterns for the following scenarios

Can any body give me some regular expression patterns which will extract the version numbers(1.5) from some sample strings like 任何人都可以给我一些正则表达式模式,这些模式将从某些示例字符串中提取版本号(1.5)

    "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. 如果与任何存储的模式都匹配,则输出应为版本号1.5。 I want to extract the version numbers (1.5) only from the above strings. 我只想从上述字符串中提取版本号(1.5)。

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" 有效的字符串格式:“ jdk1.5”,“ jdk-v1.5”,“ jdk-V1.5”,“ jdk V1.3”,“ jdkv1.4”,“ jdk version1.5”,“ 1.5.6” “,” v1.5“,” V1.5“,”版本1.5“,”版本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." 无效的字符串格式:“ 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. 如果设置了CASE_INSENSITIVE标志,则可以用v省略[vV]

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 看到它在这里工作: 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 如果有人可以帮助我做得更好,以及如何使其与1.5.6匹配,我将不胜感激。

Requirements 要求

If you can formulize what you want as: 如果您可以制定自己想要的形式:

  1. Anything in format 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.51.5.61.5.6.71.5.6.7.8等的任何内容都是有效的版本号(数字可以是一个或多个数字,如12.60.70 )。
  2. A version number 1.5 can be followed by a dot ( . ) only if it is in the format 1.5.6 . 版本号1.5可以跟随一个点( . )仅当它是格式1.5.6
  3. A version number 1.5 never preceded by a dot ( . ) like .1.5 . 版本号1.5绝对不能在.1.5之前加点号( . )。

Solution

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. 看到它在行动 ,包括所有的阳性样品,不包括你提供的所有阴性样品。

How to use 如何使用

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. 注意:这仅适用于Java 7+,因为较早的版本不支持预读。

(?<![\\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 如果要捕获两个以上的数字,例如“ 1.5.6”或“ 1.5.0”,则可以使用

[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. 或者,如果您只希望前两位数字[0-9].[0-9] ,但不适用于1.5.0.0之类的字符串,它将捕获1.5和0.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM