英文:
Trying to write a regex to get the version details from the 3.0.0.10240-8423651
问题
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VersionValidator {
public static void main(String[] args) {
// This should print the "3.0.0.10240"
System.out.println(getVersion("3.0.0.10240-8423651"));
// This should print the "3.0.0.10240"
System.out.println(getVersion("3.0.0.10240"));
// This should print the "3.0.0"
System.out.println(getVersion("3.0.0-8423651"));
}
/**
*
* 3.0.0.10240 -> 3.0.0.10240
*
* 3.0.0.10240 -> 3.0.0.10240
*
* 3.0.0-8423651 -> 3.0.0
*
*/
public static String getVersion(String version) {
Pattern vp = Pattern.compile("(\\d\\.\\d\\.\\d)(\\.\\d+)?(-\\d+)?");
Matcher vm = vp.matcher(version);
if (vm.matches()) {
return vm.group(1);
}
return null;
}
}
英文:
I am trying to write the regex to fetch just the version details. It seems like I am missing something in the regex. Appreciate your help.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VersionValidator {
public static void main(String[] args) {
// This should print the "3.0.0.10240"
System.out.println(getVersion("3.0.0.10240-8423651"));
// This should print the "3.0.0.10240"
System.out.println(getVersion("3.0.0.10240"));
// This should print the "3.0.0"
System.out.println(getVersion("3.0.0-8423651"));
}
/**
*
* 3.0.0.10240-8423651 -> 3.0.0.10240
*
* 3.0.0.10240 -> 3.0.0.10240
*
* 3.0.0-8423651 -> 3.0.0
*
*/
public static String getVersion(String version) {
Pattern vp = Pattern.compile("(\\d\\.\\d\\.\\d)(\\.\\d+)?(-\\d+)?");
Matcher vm = vp.matcher(version);
if (vm.matches()) {
return vm.group(1);
}
return null;
}
}
答案1
得分: 1
好的,以下是您要翻译的内容:
似乎对我来说,你只是在追求任何出现在前面的内容,如果它存在的话。
即,
String pattern = "^([\\.0-9]+)(?:-[0-9]+)?$";
英文:
It seems to me you are just after whatever is in front of the - if it is present.
i.e.
String pattern = "^([\.0-9]+)(?:-[0-9]+)?$";
答案2
得分: 1
以下是翻译好的部分:
你的要求的正则表达式是 (?<![^\d])\b(?:\d[\d.]*)
点击此处查看演示和解释。
正则表达式解释:
- 负向后顾
(?<![^\d])
\b
用于单词边界- 非捕获组
(?:\d[\d.]*)
查阅 java.util.regex.Pattern
以了解更多有关这些模式的信息。您还可以查看Oracle的正则表达式教程。
除了正则表达式之外,您代码中的另一个问题是使用 Matcher#matches
而不是 Matcher#find
。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
System.out.println(getVersion("3.0.0.10240-8423651"));
}
public static String getVersion(String version) {
Pattern vp = Pattern.compile("(?<![^\\d])\\b(?:\\d[\\d.]*)");
Matcher vm = vp.matcher(version);
if (vm.find()) {
return vm.group();
}
return null;
}
}
输出:
3.0.0.10240
英文:
The regex for your requirement is (?<![^\d])\b(?:\d[\d.]*)
Check this for a demo and explanation.
Explanation of the regex:
- Negative Lookbehind
(?<![^\d])
\b
is used for word boundary- Non-capturing group
(?:\d[\d.]*)
Check java.util.regex.Pattern
to learn more about these patterns. You may also like to check Regular Expressions Tutorials by Oracle.
Apart from the regex, another problem in your code is using Matcher#matches
instead of Matcher#find
.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
System.out.println(getVersion("3.0.0.10240-8423651"));
}
public static String getVersion(String version) {
Pattern vp = Pattern.compile("(?<![^\\d])\\b(?:\\d[\\d.]*)");
Matcher vm = vp.matcher(version);
if (vm.find()) {
return vm.group();
}
return null;
}
}
Output:
3.0.0.10240
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论