strmatch
-- match a pattern in
a character stringstrmatch(
text, pattern)
checks whether the
strings text
and pattern
coincide. The
pattern may contain wildcards.
strmatch(
text, pattern, Index)
checks whether the text contains the
pattern as a substring. If so, the position of the first occurrence of
pattern
is returned.
strmatch(text, pattern)
strmatch(text, pattern, Index)
text, pattern |
- | character strings |
Index |
- | makes strmatch look for substrings in
text coinciding with the pattern. If the pattern is not
found, FALSE is
returned. Otherwise, the location of the first occurrence is returned
as a list of two integers. |
Without Index, either TRUE
or FALSE
is returned. With Index, a list of two nonnegative integers or FALSE
is returned.
text
, pattern
_concat
, length
, substring
, stringlib::contains
, stringlib::pos
\?
and
\*
. The wildcard \?
represents any single
character or no character. The wildcard \*
represents
arbitrary (possibly empty) substrings.text
must not contain wildcards.\
is
represented by \\
. Cf. example 3.stringlib
provides further
functions for handling strings.strmatch
is a function of the system kernel.strmatch
(text, pattern, Index)
checks whether text
contains
the string pattern
as a substring. If so, a list [i,
j]
is returned. The integer i
is the index of the
first character of the matching substring, j
is the index
of the last character. I.e., substring
(text, i, j-i+1) =
pattern
. Only the first occurrence of pattern
inside text
is found. If no match is found,
FALSE
is returned.Note that indexing of the characters in
text
starts with 0.
pattern
, then the
largest match is found. E.g., in the text
"XXabcbXX"
, the pattern "a\*b"
matches the
substring "abcb"
rather than the substring
"ab"
.We do a simple comparison of strings:
>> s := "Hamburg": strmatch(s, "Hamburg")
TRUE
>> strmatch(s, "Ham"), strmatch(s, "burg")
FALSE, FALSE
>> delete s:
This example demonstrates wildcards. The wildcard
\?
represents a single character or no character:
>> strmatch("Mississippi", "Miss\?issip\?i")
TRUE
The wildcard \*
represents any string
including the empty string:
>> strmatch("Mississippi", "Mi\*i\*pp\*i\*")
TRUE
In the following call, no match is found:
>> strmatch("Mississippi", "Mis\?i\*ppi\*i")
FALSE
The character ?
is not a wildcard:
>> strmatch("Mississippi", "Miss?issip\?i")
FALSE
In MuPAD strings, the character \
is
represented as \\
: Consequently, \\
is
regarded as a single character:
>> s := "a\\b": s[0], s[1], s[2]
"a", "\\", "b"
>> strmatch("Missi\\ssippi", "Missi\?ssippi")
TRUE
>> delete s:
With the option Index, you can check whether a string contains another string. If so, the position of the substring in the source string is returned:
>> strmatch("cdxxcd", "xx", Index)
[2, 3]
Only the first occurrence of the pattern is found:
>> strmatch("cdxxcd", "cd", Index)
[0, 1]
The largest match is found:
>> strmatch("cdxxcxcd", "x\*x", Index)
[2, 5]
>> strmatch("cdxxcd", "xx\*", Index)
[2, 5]
>> strmatch("cdxxcd", "\*xx\*", Index)
[0, 5]
text
.