Regular Expressions in COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. REGULAR-EXPRESSIONS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STANDARD-OUTPUT ASSIGN TO DISPLAY
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD STANDARD-OUTPUT.
01 OUTPUT-LINE PIC X(80).
WORKING-STORAGE SECTION.
01 WS-PATTERN PIC X(20) VALUE "p([a-z]+)ch".
01 WS-INPUT-STRING PIC X(20) VALUE "peach".
01 WS-MATCH-RESULT PIC X(5).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM TEST-MATCH
PERFORM FIND-STRING
PERFORM REPLACE-STRING
STOP RUN.
TEST-MATCH.
CALL "CBL_OC_REGEXP" USING
WS-PATTERN
WS-INPUT-STRING
WS-MATCH-RESULT
END-CALL
IF WS-MATCH-RESULT = "TRUE"
DISPLAY "Match found: TRUE"
ELSE
DISPLAY "Match found: FALSE"
END-IF.
FIND-STRING.
CALL "CBL_OC_REGEXP" USING
WS-PATTERN
"peach punch pinch"
WS-MATCH-RESULT
END-CALL
DISPLAY "Found string: " WS-MATCH-RESULT.
REPLACE-STRING.
CALL "CBL_OC_REGEXP" USING
WS-PATTERN
"a peach"
"<fruit>"
WS-MATCH-RESULT
END-CALL
DISPLAY "Replaced string: " WS-MATCH-RESULT.
In COBOL, regular expressions are not natively supported. However, some COBOL compilers provide extensions or library functions to work with regular expressions. In this example, we’re using a hypothetical CBL_OC_REGEXP
function to demonstrate regular expression operations.
Here’s a breakdown of the COBOL program structure and the regular expression operations:
The program structure is defined with the standard COBOL divisions: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE.
We define a pattern
p([a-z]+)ch
in the WORKING-STORAGE SECTION.In the PROCEDURE DIVISION, we perform three main operations:
a. TEST-MATCH: This checks if the pattern matches a given string.
b. FIND-STRING: This finds the first occurrence of the pattern in a longer string.
c. REPLACE-STRING: This replaces the matched pattern with a new string.
The
CBL_OC_REGEXP
function is called with different parameters to perform these operations.Results are displayed using the DISPLAY statement.
Note that COBOL’s support for regular expressions is limited compared to more modern languages. The exact syntax and capabilities may vary depending on the COBOL compiler and version you’re using. Some COBOL environments might require additional configuration or external libraries to support regular expressions.
For more advanced regular expression operations, you might need to use a specialized COBOL library or consider interfacing with a language that has better regex support.