Interested in improving this site? Please check the To Do page.

Back to String Verbs

string.patternMatch

Locates a pattern in a string. String.patternMatch now has an optional parameter called “startingAt”. The old behavior is maintained in exactly the same way, but now you can also give it a position in the string at which it will start scanning.

This allows you to find matches anywhere in the string, rather than only the first match in the string. Before, if you wanted to match any occurrence of a pattern after the first match, you’d have to continually chop off the beginning of the string (up to and including the first matched character). This change will make it quite easy for some scripts to be immediately optimized.

Syntax

string.patternMatch (pattern, string, startingAt = 1)

Params

pattern is a character or string of one or more characters to be searched for in string.

string is the string to be searched in an effort to find pattern.

startingAt is the position in the string where the scan is to begin.

Returns

A number indicating the position of pattern in string if found; otherwise, returns 0.

Examples

string.patternMatch ("y", "Rudy Kiesler")

   » 4

string.patternMatch ("z", "Rudy Kiesler")

   » 0

The following example extracts characters starting at position found with string.patternMatch and returns “43”.

t = "1:43";
string.mid (t, string.patternMatch (":", t) + 1, 2);

   » "43"

The following example shows the use of the startingAt parameter.

string.patternMatch( "b", "abcde abcde" )
   >> 2

string.patternMatch( "b", "abcde abcde", 3 )
   >> 8

Notes

Return value is 1-based (i.e., the first character in a string is 1).

Reference


Personal Tools