Combining string and positional patterns: a special case
There is a special case in which absolute and relative positional patterns do not work identically.
About this task
We have shown how parsing with a template that contains a string pattern skips over the data in
the source string that matches the pattern (see Templates that contain string patterns). But a template that
contains the following sequence does not skip over the matching data.:
- string pattern
- variable name
- relative positional pattern
A relative positional pattern moves relative to the first character matching a string pattern. As a result, assignment includes the data in the source string that matches the string pattern.
/* Template containing string pattern, then variable name, then */
/* relative positional pattern does not skip over any data. */
string='REstructured eXtended eXecutor'
parse var string var1 3 junk 'X' var2 +1 junk 'X' var3 +1 junk
say var1||var2||var3 /* Concatenates variables; displays: "REXX" */
Here is how this template works:
|var1 3| |junk 'X'| |var2 +1| |junk 'X'| |var3 +1 | | junk |
+-------+ +--------+ +-------+ +---------+ +--------+ +------+
| | | | | |
Put Starting Starting Starting Starting Starting
characters at 3, put with first with char- with with char-
1 through characters 'X' put 1 acter after second 'X' acter
2 in var1. up to (not (+1) first 'X' put 1 (+1) after sec-
(Stopping including) character put up to character ond 'X'
point is first 'X' in var2. second 'X' in var3. put rest
3.) in junk. in junk. in junk.
var1='RE' junk= var2='X' junk= var3='X' junk=
'structured e' 'tended e' 'ecutor'