我需要RegEX帮助...基本上是一个匹配以下字符串的模式
G1:k6YxekrAP71LqRv[P:3]
G1:k6YxekrAP71LqRv[S:2,3,4|P:3]
G1:k6YxekrAP71LqRv[P:3|R:2,3,4,5]
G1:k6YxekrAP71LqRv[S:2,3,4|P:3|R:2,3,4,5]
“G1:k6YxekrAP71LqRv”和“P:3”是匹配的主要内容
我已经完成了以下匹配第一个字符串,但其余的都丢失了。
G1:k6YxekrAP71LqRv(\[|\|)P:3(\||\])
答案 0 :(得分:1)
如果我没有弄错的话,字符串都以G1:k6YxekrAP71LqRv
开头。
之后,它本身就有[P:3]
,或者左S:2,3,4|
,右|R:2,3,4,5
或左右都有(?:\d,)+\d
。值2,3,4和2,3,4,5可以是重复数字除以逗号。
要匹配您可以使用的完整模式:
(G1:k6YxekrAP71LqRv)\[(?:S:(?:\d,)+\d\|)?(P:3)(?:\|R:(?:\d,)+\d)?\]
<强>解释强>
(G1:k6YxekrAP71LqRv) # Match literally in group 1 \[ # Match [ (?: # Non capturing group S: # Match literally (?:\d,)+\d\| # Match repeatedly a digit and comma one or more times \d\| # Followed by a digit and | )? # Close group and make it optional (P:3) # Capture P:3 in group 2 (?: # Non capturing group \|R: # match |R: (?:\d,)+ # Match repeatedly a digit and comma one or more times \d # Followed by a digit )? # Close group and make it optional \] # Match ]
对于2,3,4
,你也可以使用2,3,4,5
和P:3
来匹配那些字面意思。
要将整个字符串与G1:k6YxekrAP71LqRv在开头匹配并且应包含(?=.*P:3)
,您可以使用正向前瞻{{1}}:
答案 1 :(得分:0)
"((G1:k6YxekrAP71LqRv)\\[.*(?<=\\||\\[)P:3(?=\\]|\\,|\\|)[^\\]]*\\])"
\\
- 在正则表达式中使用它来转义在正则表达式中具有特殊含义的字符
G1:k6YxekrAP71LqRv
这些字符需要按字面匹配(在括号中匹配组#1(&#34; ()
&#34;)
\\[.*
- [
字符及其后的任何字符零次或多次
(?<=\\||\\[)P:3
- 积极的观察 - 在这里你要说,你希望P:3
前面有|
或[
AND
P:3(?=\\]|\\,|\\|)
- 积极前瞻 - P:3
仅由]
或,
或|
跟进(如果您不想匹配)例如:P:3,4
,只需从正则表达式中删除以下部分:|\\,
(P:3
) - 捕获组#2
[^\\]]*
- 除]
\\]
- ]
个字符
String s1 = "G1:k6YxekrAP71LqRv[P:3]";
String s2 = "G1:k6YxekrAP71LqRv[S:2,3,4|P:3]";
String s3 = "G1:k6YxekrAP71LqRv[P:3|R:2,3,4,5]";
String s4 = "G1:k6YxekrAP71LqRv[S:2,3,4|P:3|R:2,3,4,5]";
String withCommaAfter = "G1:k6YxekrAP71LqRv[S:2,3,4|P:3,4]";
String notMatch1 ="G1:k6YxekrAP71LqRv[P:33]";
String notMatch2 = "G1:k6YxekrAP71LqRv[S:2,3,4|P:33]";
String[] sampleStrings = new String[] {s1, s2, s3, s4, withCommaAfter, notMatch1, notMatch2}; // to store source strings and to print results in a loop
Pattern p = Pattern.compile("(G1:k6YxekrAP71LqRv)\\[.*(?<=\\||\\[)(P:3)(?=\\]|\\,|\\|)[^\\]]*\\]");
for(String s : sampleStrings) {
System.out.println("Checked String: \"" + s + "\"");
Matcher m = p.matcher(s);
while(m.find()) { // if match is found print the following line to the console
System.out.println("\t whole String : " + m.group());
System.out.println("\t G1...qRv part : " + m.group(1));
System.out.println("\t P:3 part : " + m.group(2) + "\n");
}
}
如果您希望匹配字符串withCommaAfter
,也可以获得输出(如果您不希望匹配,请从正则表达式中删除|\\,
:
Checked String: "G1:k6YxekrAP71LqRv[P:3]"
whole String : G1:k6YxekrAP71LqRv[P:3]
G1...qRv part : G1:k6YxekrAP71LqRv
P:3 part : P:3
Checked String: "G1:k6YxekrAP71LqRv[S:2,3,4|P:3]"
whole String : G1:k6YxekrAP71LqRv[S:2,3,4|P:3]
G1...qRv part : G1:k6YxekrAP71LqRv
P:3 part : P:3
Checked String: "G1:k6YxekrAP71LqRv[P:3|R:2,3,4,5]"
whole String : G1:k6YxekrAP71LqRv[P:3|R:2,3,4,5]
G1...qRv part : G1:k6YxekrAP71LqRv
P:3 part : P:3
Checked String: "G1:k6YxekrAP71LqRv[S:2,3,4|P:3|R:2,3,4,5]"
whole String : G1:k6YxekrAP71LqRv[S:2,3,4|P:3|R:2,3,4,5]
G1...qRv part : G1:k6YxekrAP71LqRv
P:3 part : P:3
Checked String: "G1:k6YxekrAP71LqRv[S:2,3,4|P:3,4]"
whole String : G1:k6YxekrAP71LqRv[S:2,3,4|P:3,4]
G1...qRv part : G1:k6YxekrAP71LqRv
P:3 part : P:3
Checked String: "G1:k6YxekrAP71LqRv[P:33]"
Checked String: "G1:k6YxekrAP71LqRv[S:2,3,4|P:33]"