MatchField Function

Searches a string and returns the part of it that matches a pattern element.

Syntax

MatchField (string, pattern, element)

string is the string to be searched. If string does not match pattern or is a null value, an empty string is returned.

pattern is one or more pattern elements describing string, and can be any of the pattern codes used by the Match operator. If pattern is a null value, an empty string is returned.

element is a number, n, specifying that the portion of string that matches the nth element of pattern is returned. If element is a null value, it generates a runtime error.

Remarks

pattern must contain elements that describe all the characters in string. For example, the following statement returns an empty string because pattern does not cover the substring "AB" at the end of string:

MatchField ("XYZ123AB", "3X3N", 1)

The following statement describes the whole string and returns a value of "XYZ", which is 3X, the substring that matches the first element of the pattern:

MatchField ("XYZ123AB", "3X3N...", 1)

Examples

Q evaluates to BBB:

Q = MatchField("AA123BBB9","2A0N3A0N",3)

zip evaluates to 01234:

addr = '20 GREEN ST. NATICK, MA.,01234'
zip = MatchField(ADDR,"0N0X5N",3)

col evaluates to BLUE:

inv = 'PART12345 BLUE AU'
col = MatchField(INV,"10X4A3X",2)

In the following examples the string does not match the pattern and an empty string is returned:

XYZ=MatchField('ABCDE1234',"2N3A4N",1)
XYZ=
ABC=MatchField('1234AB',"4N1A",2)
ABC=