Regex to continue matching the similar pattern

kiranNswamy :

I have JCL statement to be matched with regex pattern The statement would be like below

//name JOB optionalParam,keyword=param,keyword=param,keyword=param

Actual statement would be like below

//ADBB503  JOB ,MSGCLASS=2,CLASS=P 

//ABCD JOB Something,MSG=NTNG,CLASS=ABC

I have tried a regular expression to match in groups but the last keyword and param will have n number of times I need to continue matching till it exists.

String regex= (\/\/)(\w+)(\s+)(JOB)(\s+)(\w+)?(,)([\w+=\w+]+);

My trial is in the link given below https://regex101.com/r/gUyRMV/1

The error I am facing is only one keyword=parameter is matching. N number of keyword and parameters needs to be matched.

The fourth bird :

You could match the job statement in the first capturing group and make use of \G to get the parameters in group 2:

(?:(//\w+\s+JOB(?: \w+)?)\h*|\G(?!^)),(\w+=\w+)

Explanation

  • (?: Non capturing group
    • ( Capture group 1
      • //\w+\s+JOB Match //, 1+ word chars and JOB
      • (?: \w+)? Match optional param
    • )\h*` Close group and match 0+ horizontal whitespace chars
    • | Or
    • \G(?!^) Assert position at the end of previous match, not at the start
  • ), Close non capturing group and match ,
  • ( Capture group 2
    • \w+=\w+ Match 1+ word chars = 1 + word chars
  • ) Close group

In java

String regex = "(?:(//\\w+\\s+JOB(?: \\w+)?)\\h*|\\G(?!^)),(\\w+=\\w+)";

Regex demo | Java demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140767&siteId=1