Patterns can be included in the template. Their purpose is to break down the string into substrings prior to the actual parsing into words process. Parsing will then be performed, as previously described, on the substrings and not on the original string.
Two types of patterns are available:
Using String Patterns
The string is scanned from left to right for a substring that matches the string pattern.
The following situations may occur:
The original string is divided into two substrings. The first substring (up to, but not including, the string pattern) is parsed into words using the variables named before the string pattern on the template. Parsing continues from the character following the matched string.
Example
DO SET=%%S= THIS IS A SAMPLE STRING
DO SET=%%T= A1 A2 'SAMPLE' A3 A4 A5
DO SET=%%$PARSE %%S %%T
A match is found since the string SAMPLE is part of the original string.
The %%$PARSRC System variable, which is discussed in %%$PARSE, can be used to check if all strings specified in the template were matched during the parsing process.
The original string is divided into two substrings while the matched part of the string is excluded. Parsing of the first substring will use the variables listed before the match on the template while parsing of the second substring will use the variables listed after the match:
As a result of parsing
A1=THIS
A2=IS A
As a result of parsing:
A3=STRING
A4=NULL
A5=NULL
A match is not found. There is no substring identical to the given string pattern within the string.
It is assumed that a match is found at the end of the string. The first substring consists of the entire string and it is parsed using only the variables named before the string pattern on the template. Parsing continues from the character following the matched string (the end of the string, in this case).
Example
DO SET=%%S = THIS IS A SAMPLE STRING
DO SET=%%T = A1 A2 A3 'EASY' A4 A5
DO SET=%%$PARSE %%S %%T
A match was not found. The string ‘EASY’ does not exist within the original string.
As a result of parsing:
A1=THIS
A2=IS
A3=A SAMPLE STRING
As a result of parsing:
A4=NULL
A5=NULL
Using Numeric Patterns Within the Template
Numeric patterns are numbers that mark positions in the string. They are used to break the original string into substrings at the position indicated by the number.
The position specified can be absolute or relative:
Example 1
A parsing template with an absolute numeric pattern:
DO SET=%%S =THIS IS A SAMPLE STRING
DO SET=%%T = A1 A2 11 A3 A4 A5
DO SET=%%$PARSE %%S %%T
As a result of parsing
A1=THIS
A2=IS A
As a result of parsing
A3=SAMPLE
A4=STRING
A5=NULL
(0 length string)
Example 2
A parsing template with a relative numeric pattern:
DO SET=%%S =THIS IS A SAMPLE STRING
DO SET=%%T = A1 A2 +10 A3 A4 A5
DO SET=%%$PARSE %%S %%T
Last position is the beginning of the string (position 1).
Position marked within the string is 1 + 10 = 11.
As a result of parsing
A1=THIS
A2=IS A
As a result of parsing:
A3=SAMPLE
A4=STRING
A5=NULL
(0 length string)
Using More Than One Pattern and Combining Pattern Types in the Template
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 |