[英]Java Pattern matcher and 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(\||\])
如果我沒記錯的話,字符串都以G1:k6YxekrAP71LqRv
。
在那之后,有[P:3]
本身,或者左S:2,3,4|
, 右|R:2,3,4,5
或左右。 值 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 ]
對於(?:\\d,)+\\d
你也可以使用2,3,4
和2,3,4,5
fi 來匹配它們。
要將整個字符串與開頭的 G1:k6YxekrAP71LqRv 匹配並應包含P:3
,您可以使用正向前瞻(?=.*P:3)
:
"((G1:k6YxekrAP71LqRv)\\\\[.*(?<=\\\\||\\\\[)P:3(?=\\\\]|\\\\,|\\\\|)[^\\\\]]*\\\\])"
\\\\
- 在正則表達式中用於轉義在正則表達式中具有特殊含義的字符
G1:k6YxekrAP71LqRv
這些字符需要逐字匹配(括號中的匹配組#1 (" ()
")
\\\\[.*
- [
字符及其后的任何字符零次或多次
(?<=\\\\||\\\\[)P:3
- 正向后視 - 在這里你說,你希望P:3
前面有|
或[
和
P:3(?=\\\\]|\\\\,|\\\\|)
- 正向預測 - P:3
后面只能跟]
OR ,
OR |
(如果您不想匹配例如: 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");
}
}
如果您希望 String 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]"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.