Both types of patterns (string and numeric) can be combined in the same template. Up to 30 patterns and up to 30 variable names can be specified.
Scanning of the string proceeds from beginning of the string until the first pattern (if any).
The substring that precedes the match to the pattern is parsed using the variables named in the template before the pattern, with the last variable receiving the end of the substring, including leading and trailing blanks.
Since no match was found in the string, it is assumed that a match is found at the end of the string. The whole string is parsed using only the variables named in the template before the pattern.
The absolute numeric pattern points to a position within the string when the beginning of the string is position 1.
The string is divided into two substrings.
The relative numeric pattern (a signed number) specifies a position within the string, relative to the last position.
It is the beginning of the string when the relative numeric pattern is the first pattern in the template.
As a result of what was just explained:
Example 1
A parsing template with two absolute numeric patterns (with the second position preceding the first):
The following DO SET statements:
DO SET=%%S = THIS IS A SAMPLE STRING
DO SET=%%T = A1 A2 11 A3 6 A4
DO SET=%%$PARSE %%S %%T
have the same result as the following DO SET statements:
DO SET=%%A1 = THIS
DO SET=%%A2 = IS A
DO SET=%%A3 = SAMPLE STRING
DO SET=%%A4 = IS A SAMPLE STRING
As a result of parsing:
A1=THIS
A2=IS A
As a result of parsing:
A3=SAMPLE STRING
As a result of parsing:
A4=IS A SAMPLE STRING
Example 2
A parsing template with one absolute and one relative numeric pattern:
DO SET=%%S = THIS IS A SAMPLE STRING
DO SET=%%T = A1 6 A2 +3 A3
DO SET=%%$PARSE %%S %%T
As a result of parsing:
A1=THIS
As a result of parsing
A2=IS
As a result of parsing:
A3=A SAMPLE STRING
Example 3
A parsing template with two relative numeric patterns:
The following DO SET statements
DO SET=%%T = A1 A2 +40 A3 -13 A4 A5
DO SET=%%S = THIS IS A SAMPLE STRING
DO SET=%%$PARSE %%S %%T
have the same result as the following DO SET statements:
DO SET=%%A1 = THIS
DO SET=%%A2 = IS A SAMPLE STRING
DO SET=%%A3 = %%NULL
DO SET=%%A4 = SAMPLE
DO SET=%%A5 = STRING
The first numeric pattern specifies a position at column 40. This is beyond the end of the string so the position is reset to column 24 (end of the string + 1). As a result, the whole string is parsed to words using the A1 and A2 variables.
The second numeric pattern specifies a position at column 11 (end of the string + 1 minus 13) that precedes the position (40 readjusted to 24) previously specified; therefore the data from the last position (which is the end of the string) to the end of the string is parsed to words using the A3 variable (A3 is set to NULL).
The data (from column 12 to the end of the string) is parsed to words using the A4 and A5 variables.
Example 4
Combining a string pattern and numeric pattern
The following DO SET statements
DO SET=%%S = THIS IS A SAMPLE STRING
DO SET=%%T = A1 'A' A2 +3 A3
DO SET=%%$PARSE %%S %%T
have the same result as the following DO SET statements:
DO SET=%%A1 = THIS IS
DO SET=%%A2 = A S
DO SET=%%A3 = AMPLE STRING
The pattern specifies a string (A) that is matched at column 9. The data before column 9 is parsed to words using the A1 variable. The Numeric pattern (+3) specifies a position at column 12 by using relative position. The data from column 9 to column 12 is parsed to words using the A2 variable. The remaining data (from column 12 to the end of the string) is parsed to words using the A3 variable.
Parent Topic |