up: RE     index Zimbu documentation

CLASS T.regex @public

summary

     

The builtin type regex.

Example:

 regex re = RE("pat.*ern")
 IF re.matches("in here")

The regular expression syntax is equal to what is used in Javascript, see this page

The number of matching groups is limited to 20. Using more groups may cause the pattern to fail.

When using backslashes in the pattern a raw string avoids the need to double them:

 regex re = RE("\\bword\\b")
 regex re = RE(R"\bword\b")

NEW(pattern) @public  Create a new regex from pattern.
NEW(pattern, options) @public  Create a new regex from pattern with options.
$matches(text) bool @public  Returns TRUE when there is a match in text.
$match(text) string @public  Returns the matching string in text or NIL if there is no match.
$matchGroups(text) list<string> @public  Returns the matching string in text and the strings in text matched by () groups.
$matchStart(text) int @public  Returns the index of where the regexp matches in text or -1 if there is no match.
$matchAll(text) list<string> @public  Returns all the matching strings in text as a list.
$replace(text, replace) string @public  Replaces the first match in text with replace.
$replaceAll(text, replace) string @public  Replaces all matches in text with replace.
 

members (alphabetically)

     

PROC NEW(string pattern) @public

     

Create a new regex from pattern.

PROC NEW(string pattern, RE.Options options) @public

     

Create a new regex from pattern with options.

FUNC $match(string text) string @public

     

Returns the matching string in text or NIL if there is no match.

 RE("ab+").match("an abbrev")  # result: "abb"

FUNC $matchAll(string text) list<string> @public

     

Returns all the matching strings in text as a list.

 RE("ab+").matchAll("ab in abbrev")  # result: ["ab", "abb"]
If there is no match an empty list is returned.

FUNC $matchGroups(string text) list<string> @public

     

Returns the matching string in text and the strings in text matched by () groups.

 RE("a(b+)").matchGroups("an abbrev")  # result: ["abb", "bb"]
If there is no match an empty list is returned.

FUNC $matchStart(string text) int @public

     

Returns the index of where the regexp matches in text or -1 if there is no match.

 RE("ab+").matchStart("an abbrev")  # result: 3

FUNC $matches(string text) bool @public

     

Returns TRUE when there is a match in text.

FUNC $replace(string text, string replace) string @public

     

Replaces the first match in text with replace.

 result = RE("ab+").replace("an abbrev", "AB")  # result: "an ABrev"
If there is no match text is returned unmodified.

Using $ in replace has a special meaning. $& is the matched text, $1 the first match group, $2 the second match group, etc. Use $$ to get a $. See this page for the details

FUNC $replaceAll(string text, string replace) string @public

     

Replaces all matches in text with replace.

 result = RE("[ae]").replaceAll("an abbrev", "X")  # result: "Xn XbbrXv"
If there is no match text is returned unmodified.

Using $ in replace has a special meaning. $& is the matched text, $1 the first match group, $2 the second match group, etc. Use $$ to get a $. See this page for the details

license

      Copyright 2015 Bram Moolenaar All Rights Reserved.

      Licensed under the Apache License, Version 2.0. See the LICENSE file or obtain a copy at: http://www.apache.org/licenses/LICENSE-2.0