The MatchesMask function checks the string String to see if it matches a mask.

MatchesMask can be used to check strings against any syntactically correct mask - such as checking that a phone number only contains numbers, or that file names are valid.

The Mask parameter consists of literal characters, sets, and wildcards. Each literal character must match a single character in the string. The comparison to literal characters is case-insensitive.

  • Each set begins with an opening square bracket ([) and ends with a closing square bracket (]). Between the brackets are the elements of the set, where each element is a literal character or a range.

    • Ranges are specified by an initial value, a dash (-), and a final value.

    • Do not use spaces or commas to separate the elements of the set.

    • A set must match a single character in the string. The character matches the set if it is the same as one of the literal characters in the set, or if it is in one of the ranges in the set.

    • A character is in a range if it matches the initial value, the final value, or falls between the two values.

    • All comparisons are case-insensitive.

    • If the first character after the opening bracket of a set is an exclamation point (!), then the set matches any character that is not in the set.

    • If wildcards are not used, then the length of the string given by String must match the number of characters within the Mask string exactly, else the result will be False.

  • Wildcards are asterisks (*) or question marks (?). An asterisk matches any number of characters. A question mark matches a single arbitrary character.

MatchesMask returns True if the string matches the mask, but returns False if the string does not match the mask.

If the mask is syntactically invalid, MatchesMask raises an exception error.

Declaration: Function MatchesMask( const String, Mask : string) : Boolean

Several examples follow.

procedure ScriptEvent(var Value:Variant);
var
WhatFile, WhatNum : string;
Check1, Check2, Check3 : boolean;
ReJigPh : double;
begin
WhatFile := 'ThisIsATestFile.txt';
WhatNum := '09 476 3569';
ReJigPh := ExtractNumber(WhatNum);
Check1 := MatchesMask(WhatNum,'99 999 9999'); //Returns false
Check2 := MatchesMask('5','[1-2]'); //Returns false
Check3 := MatchesMask(FloatToStr(ReJigPh), '[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]');
If Check1 = True then
LogInfo('Phone number is valid.')
else
LogInfo('Phone number is invalid.');
 
If Check2 = True then
LogInfo('Thank you.')
else
LogInfo('Sorry - it must be either 1 or 2 only.');
If Check3 = True then
LogInfo(FloatToStr(ReJigPh)+' phone number is valid.')
else
LogInfo('FloatToStr(ReJigPh)+' phone number is invalid.');
 
If MatchesMask(WhatFile, '*.txt') then //Using a wildcard
LogInfo('File name accepted.')
else
LogInfo('The file name is invalid.');
end;

The result of this code is below.