[Solved] Java Pattern matcher and RegEx


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

0

solved Java Pattern matcher and RegEx