Previous Page Next Page Contents

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

Option: Index

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




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000