尝试编写正则表达式以从3.0.0.10240-8423651中获取版本详细信息。

huangapple go评论77阅读模式
英文:

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.]*)

点击此处查看演示和解释。

正则表达式解释:

  1. 负向后顾 (?<![^\d])
  2. \b 用于单词边界
  3. 非捕获组 (?:\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:

  1. Negative Lookbehind (?<![^\d])
  2. \b is used for word boundary
  3. 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

huangapple
  • 本文由 发表于 2020年8月20日 06:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63495754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定