Parsing Fixed‑Format Text in Rexx with the PARSE Instruction
Learn how Rexx's PARSE instruction replaces manual scanning loops with a declarative template, and see a worked CSV example you can run today.
28 Apr 2026, 05:12 UTC

The problem: hand‑rolled scanning loops
When a Rexx script needs to split a line like 123,Smith,42 into three fields, the classic approach is a loop that searches for commas, tracks positions, and copies substrings. That code is verbose, error‑prone, and hard to maintain when the record layout changes.
Thesis: PARSE replaces imperative scanning with a declarative template
Rexx’s PARSE instruction lets you describe the target layout once, and the interpreter does the splitting in a single statement. The template reads left‑to‑right, uses literals as delimiters, and binds the pieces directly to variables.
How PARSE works
- Source – any string source:
VAR,ARG,PULL,VALUE,VERSION, orSOURCE. - Template – a sequence of variable names, literal strings, and positional or pattern markers (
+n,-n,=var,'literal',(expr)). - Whitespace rule – a space in the template matches any run of whitespace in the source, not a literal space.
Worked example: CSV field extraction
Save the following one‑liner as parse_csv.rex and run it with any Rexx interpreter (Regina, ooRexx, NetRexx). No special permissions are required.
/* parse_csv.rex */
parse var '123,Smith,42' id ',' name ',' age
say 'ID='id 'Name='name 'Age='age
Command to execute (Linux/macOS/Windows with Rexx in PATH):
rexx parse_csv.rex
Expected output:
ID=123 Name=Smith Age=42
You can also test the core split directly from the command line:
rexx -e "parse var 'a,b,c' x ',' y ',' z; say x y z"
This prints a b c, confirming the basic field splitting works.
Trade‑offs and limitations
- Whitespace sensitivity – a space in the template consumes any amount of whitespace in the source, which can surprise newcomers expecting literal‑space matching.
- No capture groups or back‑references – complex rearrangements often need multiple
PARSEsteps or a regex fallback. - Positional markers are 1‑based and absolute – inserting a field early shifts every later marker; keep templates next to the record layout definition.
- Case‑sensitive literals – use
UPPERon the source first if you need case‑insensitive matching.
In ooRexx and NetRexx you can still call ~match or ~find for fuzzy patterns, so you choose declarative templates for fixed formats and regex only when needed.
Actionable next step
Run the one‑liner above on your preferred Rexx implementation. If it works, replace any hand‑written scanning loops in your scripts with a PARSE template. For the authoritative syntax, consult the ANSI X3.274‑1996 standard §4.4.6 or the ooRexx 5.0 reference “PARSE instruction” section.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.