MATCHFIELD |
![]() ![]() ![]() |
The MATCHFIELD() function extracts a portion of a string that matches a pattern element.
Format
MATCHFIELD(string, pattern, element)
where
The MATCHFIELD() function matches string against pattern and returns the portion of string that matches the element'th component of pattern.
The pattern string consists of one or more concatenated items from the following list.
The values n and m are integers with any number of digits. m must be greater than or equal to n.
The 0A, nA, 0N, nN and "string" patterns may be preceded by a tilde (~) to invert the match condition. For example, ~4N matches four non-numeric characters such as ABCD (not a string which is not four numeric characters such as 12C4).
A null string matches patterns ..., 0A, 0X, 0N, their inverses (~0A, etc) and "".
The 0X and n-mX patterns match against as few characters as necessary before control passes to the next pattern. For example, the string ABC123DEF matched against the pattern 0X2N0X matches the pattern components as ABC, 12 and 3DEF.
The 0N, n-mN, 0A, and n-mA patterns match against as many characters as possible. For example, the string ABC123DEF matched against the pattern 0X2-3N0X matches the pattern components as ABC, 123 and DEF.
The pattern string may contain alternative templates separated by value marks. The MATCHFIELD() function tries each template in turn until one is a successful match against string.
The element argument determines which component of string is returned as the MATCHFIELD() function result. For example,
MATCHFIELD("ABC123DEF", "0X2N0X", 2)
returns the string "12".
The MATCHFIELD() function returns a null string if no component of pattern matches string.
Example
TEL.NO = "01604-709200" LOCAL.NO = MATCHFIELD(TEL.NO, "0N'-'0N", 3)
This program fragment extracts the local part of a telephone number (709200 in this case). Note that the literal element counts as a component of the string when identifying each element.
If the delimiter is multiple characters but not quoted, each character is counted as a separate element:
See also: |