Splitting Delimited Strings in Rexx Using the PARSE Statement
Learn how to use the Rexx PARSE statement to efficiently split delimited strings into named variables, including handling empty fields and dynamic field counts.
13 Mar 2026, 01:43 UTC

The Problem: Handling Flat-File Data
When processing CSV files or system logs, you often receive a single long string containing multiple data points separated by a character like a comma or pipe. Manually calculating the position of every delimiter using string offsets is error-prone and difficult to maintain.
The PARSE statement in Rexx solves this by allowing you to map a delimited string directly into a set of named variables in a single operation. This removes the need for complex loops or manual substring calculations.
Prerequisites
- A Rexx interpreter installed (such as Regina Rexx or IBM REXX).
- A text editor to create
.rexor.rexxscripts. - A terminal or command prompt with the interpreter in the system path.
Implementing the PARSE VAR Procedure
To split a string, you use the PARSE VAR syntax. This tells the interpreter to look at the value of a specific variable and distribute its contents into new variables based on a delimiter.
Step 1: Define the Input String
Store the raw data in a variable. Ensure the string contains the expected delimiters.
/* Example input: A comma-separated list of user attributes */
userData = "John Doe,Engineering,Level 4,Active"
Step 2: Execute the Parse Operation
Specify the source variable, the target variables, and the delimiter character. The delimiter must be enclosed in quotes.
/* Syntax: PARSE VAR source target1 target2 ... delimiter */
PARSE VAR userData name dept rank status ','
Step 3: Access the Extracted Fields
The variables defined in the PARSE statement now hold the individual pieces of data. You can use them immediately in your logic.
SAY "Employee: " name
SAY "Department: " dept
Concrete Example: Processing a CSV Line
Create a file named parse_test.rex with the following code to test the behavior of the parser, including how it handles empty fields.
/* Test 1: Standard delimited string */
line1 = 'a,b,c'
PARSE VAR line1 f1 f2 f3 ','
SAY 'Test 1 Result: ' f1 f2 f3
/* Test 2: Handling empty fields (edge case) */
line2 = 'x,,z'
PARSE VAR line2 f1 f2 f3 ','
SAY 'Test 2 Result: <'f1'> <'f2'> <'f3'>'
Running the Script
Run the script from your command line using your installed interpreter:
rexx parse_test.rex
Expected Results
| Input | Expected Output | Observation |
|---|---|---|
'a,b,c' |
a b c |
Standard split into three variables. |
'x,,z' |
<x> <> <z> |
The second variable is assigned an empty string. |
Validation and Diagnostics
To ensure the parse was successful, check for empty variables that are required for your application logic. Because Rexx does not throw an error if the number of delimiters is fewer than the number of target variables, you must validate the results manually.
Verification Check:
IF status = "" THEN DO SAY "Error: Missing status field in input string" /* Handle missing data */ ENDLimitations and Recovery
The
PARSE VARmethod has a significant limitation: it requires you to know the number of fields in advance. If the number of fields varies per line,PARSE VARwill simply leave the remaining target variables empty or truncate the input string.Recovery Option: Dynamic Splitting
If the field count is unknown, avoid
PARSE VARand use theSEPARATEinstruction or aWHILEloop combined with theFINDandSUBSTRfunctions. This allows you to push the results into a stem variable (an array-like structure) for flexible access.Example of a manual split fallback:
/* Manual split using a loop for unknown field counts */ input = "val1,val2,val3,val4,val5" count = 0 DO WHILE POS(',', input) > 0 count = count + 1 PARSE VAR input field ',' field.count = field /* The remaining part of the string is kept in 'input' */ END field.count = input /* Capture the final field */Rollback: Since
PARSEonly changes the value of variables in memory and does not modify the source string (unless you usePARSE VALUE), there is no state to roll back. To reset, simply re-assign the original source string to the variables.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.