Regular expressions application examples
Example 1: Include all records that contain string 'Department'(case-insensitive) using
Regular
expressions:
//STEP0100 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTDIAG DD DUMMY
//SORTIN DD *
lower case florida department of Palm trees
UPPER CASE FLORIDA DEPARTMENT OF ORANGE GROVES
exclude this record California state
alternate case FlOrIdA DePaRtMeNt Of PaLm TrEeS
sentence Florida Department Of Revenue
Mixed case ARIZONA DEPARTment of Motor Vehicles
//SORTOUT DD SYSOUT=*
//SYSIN DD *
OPTION COPY
INCLUDE COND=(25,50,SS,RE,C'DEPARTMENT')
//*
Using the string C’department’ or C'DePaRtMeNt' will also produce the same exact result.
This example illustrates how to include only records in which:
- DEPARTMENT (case-insensitive) is found somewhere within bytes 25 through 74
- Using the string C’department’ or C'DePaRtMeNt' will also produce the same exact results.
The Output from the above job
is:
lower case florida department of Palm trees
UPPER CASE FLORIDA DEPARTMENT OF ORANGE GROVES
alternate case FlOrIdA DePaRtMeNt Of PaLm TrEeS
sentence Florida Department Of Revenue
Mixed case ARIZONA DEPARTment of Motor Vehicles
Example 2: Omit records which contain one or more occurrences of a character or hex
strings using Regular
expressions.
//STEP0100 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD *
record - 1 1234500\x00a ANZ]'' string CD0
record - 2 12345111EAND'45678901234567890
record - 3 123452289012RRAA"890....567890
record - 4 1234533\x00a SIMPLE string CD0
record - 5 12345441EANx000! B1
record - 6 123456789012345fizZZ
record - 7 123456789012345678901234567890
/*
//SORTOUT DD SYSOUT=*
//SYSIN DD *
OPTION COPY
OUTFIL OMIT=(25,032,SS,REH,C'(.*Z+)|(.*\XC2\XF1.*)')
//*
This example illustrates how to exclude records in which:
- One or more character ‘z’ (Case-insensitive) is followed by any character OR the records that contain hex values of X’C2F1’ which is equivalent to characters ‘B1’ are found somewhere within bytes 25 through 56.
The output from this job
is:
record - 2 12345111EAND'45678901234567890
record - 3 123452289012RRAA"890....567890
record - 4 1234533\x00a SIMPLE string CD0
record - 7 123456789012345678901234567890
Example 3: Include all records that contain string 'ER'(case-sensitive) followed by a
numeric (0-9) using Regular
expressions.
//STEP0100 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD *
MORGAN STANLEY
ERIC
HOLMER CA 90201
WALTER ALBUQUERQUENEW MEXICO
TONY NY 10000
JERRY 10029 NEW YORK
SHERLOCK
//SORTOUT DD SYSOUT=*
//SYSIN DD *
OPTION COPY
INCLUDE COND=(1,50,SS,RE,C'(ER).*[0-9]')
//*
This example illustrates how to include only records in which:
- ER (case-insensitive) is found somewhere within bytes 1 through 50 and have a number (0-9) following the string ER.
The output from this job
is:
HOLMER CA 90201
JERRY 10029 NEW YORK
Example 4: Show the usage of Regular expressions with JOINKEYS
application.//STEP0100 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//INA DD DISP=SHR,DSN=Your.input.file1
//INB DD DISP=SHR,DSN=Your.input.file2
//SORTOUT DD SYSOUT=*
//SYSIN DD *
OPTION COPY
JOINKEYS F1=F1,FIELDS=(10,4,A)
JOINKEYS F2=F2,FIELDS=(10,4,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(F1:10,4,F2:10,4,F1:3,6,F2:3,6)
/*
//JNF1CNTL DD *
OMIT COND=(10,4,SS,RE,C'8D[0-9A-F]{2,2}')
/*
//JNF2CNTL DD *
OMIT COND=(10,4,CH,EQ,C' ')
/*

Note:

Regular expressions can only be used in the main task or subtask 1 or subtask 2, and not in any combination of tasks.
