MatchStr Function

Action

Looks for a pattern in a string.

Syntax

bFound = MatchStr (sPattern, sString [, eCaseSensitivity, eEscapeWildcards])
Variable Description
bFound Whether the pattern was found. BOOLEAN.
sPattern The pattern to search for. STRING.
sString The string to search. STRING.
eCaseSensitivity Defines whether the pattern is case sensitive or not.
CS_CASE_SENSITIVE
The pattern is case sensitive.
CS_CASE_INSENSITIVE
The pattern is case insensitive.

Default is CS_CASE_INSENSITIVE. ENUM.

eEscapeWildcards Defines whether to enable escaping wildcard characters with a backslash (\) in the pattern to search for.
EW_ENABLE_ESCAPE_WILDCARDS
Wildcard characters in the pattern to search can be escaped with a backslash (\), to enable comparing the actual characters.
EW_DISABLE_ESCAPE_WILDCARDS
Wildcard characters in the pattern to search for cannot be escaped with a backslash (\).

Default is EW_DISABLE_ESCAPE_WILDCARDS. ENUM.

Notes

  • MatchStr looks for sPattern in sString and returns TRUE if the pattern is in the string, or FALSE if it is not.
  • Use wildcard characters in sPattern to account for any characters that might be in the search string and are not to be considered in the match. sPattern can contain any number of the wildcard characters: question mark (?) and asterisk (*). A question mark matches any single character. An asterisk matches any string of zero or more characters. In the following example, the first asterisk accounts for the drive designation and the second asterisk accounts for the rest of the string after the ":\" characters.
  • Use MatchStr to search through a file, a listing of menu contents, or other collection of strings for a pattern.

Example

[-] testcase MatchStrExample()
	[ ] LIST OF STRING lsFileNames // list of file names
	[ ] INTEGER i // loop counter
	[-] lsFileNames = {...} 
		[ ] "output.TXT" 
		[ ] "c:\windows\partner.ini" 
		[ ] "d:\dos\*.txt" 
		[ ] "result.out"
		[ ] "test.res" 
		[ ] "*.TXT"
	[ ] 
	[ ] // Print all file names that don't have a full path associated with them.
	[-] for i = 1 to ListCount(lsFileNames)
		[-] if (!MatchStr ("*:\*", lsFileNames[i]))
			[ ] Print("File has no path: ", lsFileNames[i]) 
	[ ] Print()
	[ ] 
	[ ] // Print all file names with a TXT extension
	[-] for i = 1 to ListCount(lsFileNames)
		[-] if (MatchStr ("*.txt", lsFileNames[i], CS_CASE_INSENSITIVE))
			[ ] Print("Filename with a TXT extension: ", lsFileNames[i]) 
	[ ] Print()
	[ ] 
	[ ] // Print all file names with an uppercase TXT extension
	[-] for i = 1 to ListCount(lsFileNames)
		[-] if (MatchStr("*.TXT", lsFileNames[i], CS_CASE_SENSITIVE))
			[ ] Print("Filename with an uppercase TXT extension: ", lsFileNames[i]) 
	[ ] Print()
	[ ] 
	[ ] // Print all file names that contain an asterisk
	[-] for i = 1 to ListCount(lsFileNames)
		[-] if (MatchStr("*\**", lsFileNames[i], CS_CASE_INSENSITIVE, EW_ENABLE_ESCAPE_WILDCARDS))
			[ ] Print ("Filename contains an asterisk: ", lsFileNames[i]) 
	[ ] Print()
	[ ] 
	[ ] // Print all file names that contain have an uppercase txt extension and contain an asterisk
	[-] for i = 1 to ListCount(lsFileNames)
		[-] if (MatchStr("*\**.TXT", lsFileNames[i], CS_CASE_SENSITIVE, EW_ENABLE_ESCAPE_WILDCARDS))
			[ ] Print ("Filename contains an asterisk and uppercase TXT extension: ", lsFileNames[i]) 
	[ ] Print()