Returns the indices of a search string that contains a pattern string.
"pattern", "srchstr", "delimit", caseflag, exactflag, substrflag, skipempty, wildcards) |
"pattern" |
- |
A string. The pattern string to search for. |
||||||
"srchstr" |
- |
A string. The search string to search in. |
||||||
"delimit" |
- |
Optional. A string specifying the characters used to separate srchstr into individual substrings. Defaults to no character. |
||||||
caseflag |
- |
Optional. An integer, the case sensitivity flag.
|
||||||
exactflag |
- |
Optional. An integer, the substring must match the number of characters in the pattern exactly. Only valid if a delimiter string is specified.
|
||||||
substrflag |
- |
Optional. An integer, return the substring index instead of the character index. Only valid if a delimiter string is specified.
|
||||||
skipempty |
- |
Optional. An integer, ignore empty substrings.
|
||||||
wildcards |
- |
Optional. An integer, process * (any sequence) and ? (single character) as wildcard characters or use regular expressions (regex).
|
A series, the indices of srchstr that contain pattern.
strmatch("abb", "abbababba")
returns {1, 6} indicating the pattern "abb" is located at index 1 and 6 of "abbababba".
strmatch("AAA", "baaAAAab")
returns {2, 3, 4, 5} since the case insensitive pattern "AAA" is located at indices 2 through 5 of "baaAAAab".
strmatch("AAA", "baaAAAab", 1)
returns {4} since the case sensitive pattern "AAA" is located only at index 4.
strmatch("xxx", "baaAAAab")
returns {}, an empty series since "xxx" is not located in the search string.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 0, 0)
returns {6, 11, 15}, since "xXx" is located at indices 6, 11 and 15 where each substring is separated by the ";" character. A match occurs only if the first three characters of a substring matches "xXx" independent of case. Only one match per substring is returned.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 1, 0)
returns {6, 11}, since "xXx" is located at indices 6 and 11 where each substring is separated by the ";" character. A match occurs only if the first three characters of a substring matches "xXx" with the same case. Only one match per substring is returned.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 0, 1)
returns {11, 15}, since "xXx" is located at indices 11 and 15 where each substring is separated by the ";" character. A match occurs only if the substring contains exactly three characters and matches "xXx" independent of case. Only one match per substring is returned.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 1, 1)
returns {11}, since "xXx" is located at index 11 where each substring is separated by the ";" character. A match occurs only if the substring contains exactly three characters and matches "xXx" with the same case. Only one match per substring is returned.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 0, 1, 1)
returns {3, 4}, since "xXx" is found in the third and fourth substrings where each substring is separated by the ";" character. A match is case insensitive. Only one match per substring is returned.
strmatch("xXx", "bxxx;xXxx;xXx;xxx;xx", ";", 1, 1, 1)
returns {3}, since "xXx" is found in the third substring where each substring is separated by the ";" character. A match is case sensitive. Only one match per substring is returned.
strmatch("日本語", "abc,日本語,def,日本語", ",", 1, 1);
returns {5, 13}, since "日本語" is found at index 5 and index 13.
strmatch("日本語", "abc,日本語,def,日本語", ",", 1, 1, 1);
returns {2, 4}, since "日本語" is found at substring 2 and substring 4.
strmatch("xX*", "bxxx;xXxx;xXx;xxx;xx", ";", 1, 0, 1, 1, 1)
returns {2, 3}, since "xXxx" and "xXx" match the wildcard pattern "xX*".
strmatch("[A-Z]+", "helloWORLDfoo", "", 1, 0, 0, 1, 2);
returns {6}. The regular expression [A-Z]+ implies one or more uppercase letters A through Z.
strmatch("^[0-9]+$", "123,abc,456", ",", 0, 0, 1, 1, 2);
returns {1, 3}. The regular expression ^[0-9]+$ implies the entire string must consist of one or more digits. Since comma separated substrings are searched, the result indicates substring 1 and 3 consist of all digits.
strmatch("[0-9]+", "prix: 42€ et 7€", "", 0, 0, 0, 1, 2);
returns {7, 14}. The regular expression [0-9]+ implies match one or more digits which occurs at index 7 and index 14. The search string is not delimited.
strmatch("[0-9]+", "prix: 42€ et 7€", " ", 0, 0, 0, 1, 2);
returns an empty string since no space separated substring contains only 1 or more digits.
strmatch("[0-9]+.", "prix: 42€ et 7€", " ", 0, 0, 1, 1, 2);
returns {2, 4}. The regular expression [0-9]+. implies match one or more digits followed by any single character which occurs at space delimited substring 2 and substring 4.
If no match is found, an empty series is returned.
The "delimit", string can contain more than one delimiter characters to mark a substring within the search string.
When wildcard is set to 1, use * in "patstr" to match any character sequence and ? to match a single character. To search for a literal * or ? character instead of treating them as wildcards, escape them by prefixing a backslash: \* or \?.
When wildcard is set to 2, the search pattern "patstr" is interpreted a regular expression. Regular expressions (regex) provide far more flexibility than simple wildcards. A regular expression can indicate character classes (match a character from a set), quantifiers (how many times a pattern repeats), anchors (position in the string) and logical operators. For example, the pattern:
[0-9]{3}
matches exactly 3 digits.
a = strmatch("[0-9]{3}", "a12,123,1234,1a2,987", ",", 1, 1, 1, 0, 2)
returns {2, 5} indicating the substrings "123" and "987" have exactly 3 digits.
Below is a summary of regular expression patterns when wildcards is 2:
Category |
Character |
Description / Matches |
|---|---|---|
Character Matches |
|
Any single character except a newline (\n). |
Character Matches |
|
Any digit character (equivalent
to |
Character Matches |
|
Any alphanumeric "word" character (letters, numbers, underscore). |
Character Matches |
|
Any whitespace character (spaces, tabs, line breaks). |
Character Classes |
|
Any single character inside the brackets (a, b, or c). |
Character Classes |
|
Any single character NOT inside the brackets. |
Quantifiers |
|
0 or more repetitions of the preceding token. |
Quantifiers |
|
1 or more repetitions of the preceding token. |
Quantifiers |
|
0 or 1 repetition (makes the preceding token optional). |
Quantifiers |
|
Between |
Anchors |
|
The start of the string or line. |
Anchors |
|
The end of the string or line. |
Anchors |
|
A word boundary (the edge position between a \w and a non-\w character). |
Logic / Groups |
|
Alternation operator (acts like a logical OR statement). |
Logic / Groups |
|
Capturing group. Isolates tokens and remembers the match context. |
Digits only: ^[0-9]+$
Uppercase letters: ^[A-Z]+$
Alphanumeric: ^[A-Za-z0-9]+$
Floating‑point number: ^[+-]?\d+(\.\d+)?$
Email (simple): ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
See STRGET to return a substring given an integer index and STRIDXGET to return substrings a series of indices.