简体   繁体   中英

Java Pattern matcher and RegEx

I need RegEX help please... basically a pattern that matches the following strings

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" and "P:3" are the main thing to match

I've done the below to match the first string but got lost with the rest.

G1:k6YxekrAP71LqRv(\[|\|)P:3(\||\])

If I am not mistaken, the strings all begin with G1:k6YxekrAP71LqRv .

After that, there is [P:3] by itself, or with either left S:2,3,4| , right |R:2,3,4,5 or with both left and right. The values 2,3,4 and 2,3,4,5 could be repetitive digits divided by a comma.

To match the full pattern you could use:

(G1:k6YxekrAP71LqRv)\\[(?:S:(?:\\d,)+\\d\\|)?(P:3)(?:\\|R:(?:\\d,)+\\d)?\\]

Explanation

(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 ]

Java Demo

And for the (?:\\d,)+\\d you could also use 2,3,4 and 2,3,4,5 fi you want to match those literally.

To match the whole string with G1:k6YxekrAP71LqRv at the start and should contain P:3 , you could use a positive lookahead (?=.*P:3) :

\\AG1:k6YxekrAP71LqRv(?=.*P:3).*\\z

Solution:

"((G1:k6YxekrAP71LqRv)\\\\[.*(?<=\\\\||\\\\[)P:3(?=\\\\]|\\\\,|\\\\|)[^\\\\]]*\\\\])"

Explanation:

\\\\ - this is used in the regex to escape characters that have special meaning in regex

G1:k6YxekrAP71LqRv these characters need to be matched literally (matching group #1 in parenthesis (" () ")

\\\\[.* - [ character and after it any character zero or more times

(?<=\\\\||\\\\[)P:3 - positive lookbehind - here you say, you want P:3 to be preceded by | OR [

AND

P:3(?=\\\\]|\\\\,|\\\\|) - positive lookahead - P:3 to be followed only by ] OR , OR | (if you don't want to match eg: P:3,4 , simply delete the following part from the regex: |\\\\,

( P:3 ) - capturing group #2

[^\\\\]]* - there can appear zero or more characters other than ]

\\\\] - ] character at the end of match

Code to check pattern:

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");
    }
}

Output that you get if you want String withCommaAfter to be matched too (if you don't want it to be matched, delete |\\\\, from the regex:

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]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM