gpt4 book ai didi

java - 正则表达式捕获多个组而不考虑顺序

转载 作者:行者123 更新时间:2023-11-30 12:03:01 26 4
gpt4 key购买 nike

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

我有以下字符串,想要捕获 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的值。

最佳答案

也许,

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

可能只是返回 trackingID,您也可以为其他人做类似的事情。

Demo 1

或者用类似的表达方式,

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

Demo 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

关于java - 正则表达式捕获多个组而不考虑顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com