在 java 中获取状态码的正则表达式是什么?

what is the regex for getting statuscode in java?

下面是我从服务器获取的字符串,我想获取状态码

program 40006932 version 1 protocol tcp NOT registered
Transient program number selected = 40006932
TRANS_NUM = 999999
errorCount = 0
descriptor_loop_length= 1
descriptor_loop_val= 8828256
result_type= (null)
r_d_type= (null)
StatusCode # 0 = 0

我正在采用一种方法,首先我遍历每一行并检查 StatusCode 然后我可以将它与 = 分开并可以获得状态代码但它们是一种更简单的方法实现上述输出?

您可以搜索模式匹配:

public static void main(String[] args) {
    String test = "r_d_type= (null)\n" +
                  "StatusCode # 0 = 0";

    Pattern pattern = Pattern.compile("StatusCode.* = (\d*)");

    Matcher matcher = pattern.matcher(test);

    if (matcher.find()) {
        System.out.println("status code: " + matcher.group(1));
    }
}