Previous Topic

Next Topic

Book Contents

Book Index

Using Patterns in Parsing

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:

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:

Last position is the beginning of the string (position 1).

Position marked within the string is 1 + 10 = 11.

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).

  1. String pattern – A match was found

    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.

  2. String pattern – A match was not found

    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.

  3. Numeric pattern (absolute)

    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.

  4. Relative numeric pattern

    The relative numeric pattern (a signed number) specifies a position within the string, relative to the last position.

  5. 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:

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

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

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

%%$PARSE