您可以使用正则表达式来提取字符串中的特定值。下面是一个使用正则表达式来获取 'zxc_hdfs_002' 的示例代码:

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

public class Main {
    public static void main(String[] args) {
        String input = '(0) Validation failure: error code[3010], reason[Another policy already exists for matching resource: policy-name=[zxc_hdfs_002], service=[hadoop_240]], field[resources], subfield[null], type[semantically incorrect]';

        // 定义正则表达式
        String regex = 'policy-name=\[([^\]]+)]';
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        // 查找匹配的值
        if (matcher.find()) {
            String value = matcher.group(1);
            System.out.println(value); // 输出 zxc_hdfs_002
        } else {
            System.out.println('未找到匹配的值');
        }
    }
}

运行上述代码将输出:zxc_hdfs_002。这里使用的正则表达式是 'policy-name=\[([^\]]+)]',它匹配以 policy-name=[ 开头、] 结尾的字符串,并提取中间的值。

Java 代码提取字符串中特定值:从错误信息获取 'zxc_hdfs_002'

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

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