首页 > 解决方案 > 正则表达式捕获多个组,不考虑顺序

问题描述

想要从单词以不同顺序出现的字符串中捕获多个单词。我试图用下面的正则表达式来捕捉这些词。

我有以下字符串,想要捕获 trackingID、代码、味精和状态。所有这些词都以不同的顺序出现。

ErrorString : trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"messages\":\"msg:INVALID_ERROR\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"$httpStatus\":400},\"status\":400,\"test\":\"ABC2342fdsff3\"

ErrorString : key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"$httpStatus\":400},\"messages\":\"msg:INVALID_ERROR\",\"status\":400,\"test\":\"ABC2342fdsff3\"trackingID=jskdf-77sdkj-oij4-kerj43-lklfds;

ErrorString : trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"status\":400,\"$httpStatus\":400},\"messages\":\"msg:INVALID_ERROR\",\"test\":\"ABC2342fdsff3\"

尝试使用此正则表达式:

(trackingID|\\"code\\"|\\"msg|\\"status\\")(=|:\\"|:)([^(;|\\"|,)]*)

但它不起作用。

想要捕获 trackingID、code、msg 和 status 的值。

标签: javaregexperformance

解决方案


也许,

(?<=\btrackingID\b=)\s*[^;\r\n]*

可能会简单地返回trackingID,你可以为其他人做类似的事情。

演示 1

或者用一些类似的表达方式,

(?<=\btrackingID\b=)(\s*[^;\r\n]*)|\\"code\\":\\"([^\\"\r\n]*)|(\\"status\\"):(\d*)|\\"msg:([^\\"\r\n]*)

演示 2

测试

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


public class re{

    public static void main(String[] args){

        final String regex = "(?<=\\btrackingID\\b=)(\\s*[^;\\r\\n]*)|\\\\\"code\\\\\":\\\\\"([^\\\\\"\\r\\n]*)|(\\\\\"status\\\\\"):(\\d*)|\\\\\"msg:([^\\\\\"\\r\\n]*)";
        final String string = "ErrorString :  trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"$httpStatus\\\":400},\\\"status\\\":400,\\\"test\\\":\\\"ABC2342fdsff3\\\"\n\n\n\n"
             + "ErrorString :   key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"$httpStatus\\\":400},\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"status\\\":400,\\\"test\\\":\\\"ABC2342fdsff3\\\"trackingID=jskdf-77sdkj-oij4-kerj43-lklfds;\n\n\n\n"
             + "ErrorString :  trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"status\\\":400,\\\"$httpStatus\\\":400},\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"test\\\":\\\"ABC2342fdsff3\\\"";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

输出

Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR

推荐阅读