您可以使用正则表达式来提取所需的值。以下是一个示例代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String str1 = "0.7.3-grg-0.12.3";
        String str2 = "10.7.23223-grg-0.12.3";

        String regex = "\\d+(\\.\\d+)+";
        Pattern pattern = Pattern.compile(regex);

        Matcher matcher1 = pattern.matcher(str1);
        if (matcher1.find()) {
            String value1 = matcher1.group();
            System.out.println(value1);
        }

        Matcher matcher2 = pattern.matcher(str2);
        if (matcher2.find()) {
            String value2 = matcher2.group();
            System.out.println(value2);
        }
    }
}

输出结果为:

0.7.3
10.7.23223

代码中使用了正则表达式 \d+(\.\d+)+,它匹配一个或多个数字,后面跟着一个或多个以点号分隔的数字。使用 Pattern.compile 方法将正则表达式编译成模式对象,然后使用 Matcher 对象进行匹配。Matcher.find 方法用于在字符串中查找匹配的部分,Matcher.group 方法返回找到的匹配结果。

编写java 代码从这个 字符串中073-grg-0123 获取 073 这个值 或从这个 字符串中10723223-grg-0123 获取10723223 这个值

原文地址: https://www.cveoy.top/t/topic/i9zj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录