0
votes

I am trying to match a regex pattern for a JCL job statement Which can come in the below formats

//jobname JOB
//jobname JOB AccountInfo,'PGMR name',keyword=param,keyword=param,keywod=param

//jobname JOB ,keyword=param,keyword=param

//jobname JOB (Accountinfo)

//jobname JOB 'pgmrname'

//jobname JOB accountinfo

The structure is explained below

//name job > mandatory fields

Account info is optional and can exiting in two day accountinfo or (accountinfo)

PGMR name is optional and looks like this 'myname'

key word parameters optional and will be in pairs MSGCLASS=1,CLASS=P

Looking for matching the regex to match above all optional combination

I have tried using ?= if then else condition but not working regex used in given below

 \/\/\w+\s+JOB\s+(?:(?=(\w+|'\w+')(,\w+)|\w+))?(,(\w+=\w+))$

visualization of rgex conditon

Visalization of my regex is given above

Matching and non matching strings are given in below trail demo Not able to achieve the required result. https://regex101.com/r/CdpB9c/2

2
Yeah working. But I am unable to decode it and understand.kiranNswamy
Does it give you the expected matches? I can add it as an answer if you want with an explanation.The fourth bird
Yes it gives expected matches. Testing further more possible valid combinations. Please add the explanation as an answer.kiranNswamy

2 Answers

1
votes

To get a full match for the examples, you might make use of the \G anchor:

^(//\w+\s+JOB[^,\r\n]*(?:,(?:[^,=\r\n]*,)?)?)(\w+=\w+(?:,\w+=\w+)*)?

I have created to capturing groups to get the 2 different parts, those can also be omitted.

  • ^ Start of string
  • ( Capture group 1
    • //\w+\s+JOB[^,\r\n]* Match the start including JOB, then match not a newline or ,
    • (?:,(?:[^,=\r\n]*,)?)? Optionally match a , and optional part without an = or ,
  • )
  • ( Capture group 2
    • \w+=\w+(?:,\w+=\w+)* Match key=value pairs consisting of word characters
  • )? Close group and make it optional as it is not always there

Regex demo

To get separate parts, you could use the \G anchor:

  • (?: Non capturing group

    • ^//\w+\s+JOB\h* Match start of string till JOB and 0+ horizontal whitespace chars
    • (?:[^\s=]+)? Optionally match any char except a whitespace char or =
    • (?:,|$) Match , or end of string
    • | Or
    • \G(?!^) Assert position at the end of previous match, not at the start
  • ) Close non capturing group
  • (?:\w+=\w+)? Optionally match key=value part consisting of word chars

Regex demo

0
votes

I think something is missing from what you are trying to explain:

Do you need to have capture groups to extract information? Do you need to select just one line at a time?

if you just need to capture everything that start with \jobname JOB PARAMS, then you can simply say: \/\/.*JOB\s*.* but im almost certain that this is not the case and some other explanation is missing.

If you need to get parts of the job string and split it into capture groups, something like this might be a good starting point:

\/\/(.*) JOB (('[a-zA-Z]*')*|(\([a-zA-Z]*\))*|([a-zA-Z]*=[a-zA-Z]*)|(,)|([a-zA-Z]*))*(\n|$)

I think it needs some polishing, but it extracts most cases into separate groups.