## `strmatch` -- match a pattern in a character string

#### Introduction

`strmatch(`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.

#### Call(s)

`strmatch(text, pattern)`
`strmatch(text, pattern, Index)`

#### Parameters

`text, pattern` | - | character strings

#### Options

`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.

#### Returns

Without Index, either `TRUE` or `FALSE` is returned. With Index, a list of two nonnegative integers or `FALSE` is returned.

### Overloadable:

`text`, `pattern`

#### Related Functions

`_concat`, `length`, `substring`, `stringlib::contains`, `stringlib::pos`

#### Details

- The pattern may contain the wildcards `\?` and `\*`. The wildcard `\?` represents any single character or no character. The wildcard `\*` represents arbitrary (possibly empty) substrings.
- The string `text` must not contain wildcards.
- In MuPAD strings, the character `\` is represented by `\\`. Cf. example 3.
- The library `stringlib` provides further functions for handling strings.
- `strmatch` is a function of the system kernel.

#### Option: Index

- `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.

- If a wildcard is used in `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"`.

#### Example 1

We do a simple comparison of strings:

    >> s := "Hamburg": strmatch(s, "Hamburg")

                                       TRUE

    >> strmatch(s, "Ham"), strmatch(s, "burg")

                                   FALSE, FALSE

    >> delete s:

#### Example 2

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

#### Example 3

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:

#### Example 4

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]

#### Changes

- In previous MuPAD versions, it was possible to have wildcards inside the string `text`.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000