RegEx

Inherits:RefCounted<Object

Class for searching text for patterns using regular expressions.

Description

A regular expression (or regex) is a compact language that can be used to recognize strings that follow a specific pattern, such as URLs, email addresses, complete sentences, etc. For example, a regex ofab[0-9] would find any string that isab followed by any number from0 to9. For a more in-depth look, you can easily find various tutorials and detailed explanations on the Internet.

To begin, the RegEx object needs to be compiled with the search pattern usingcompile() before it can be used.

varregex=RegEx.new()regex.compile("\\w-(\\d+)")

The search pattern must be escaped first for GDScript before it is escaped for the expression. For example,compile("\\d+") would be read by RegEx as\d+. Similarly,compile("\"(?:\\\\.|[^\"])*\"") would be read as"(?:\\.|[^"])*". In GDScript, you can also use raw string literals (r-strings). For example,compile(r'"(?:\\.|[^"])*"') would be read the same.

Usingsearch(), you can find the pattern within the given text. If a pattern is found,RegExMatch is returned and you can retrieve details of the results using methods such asRegExMatch.get_string() andRegExMatch.get_start().

varregex=RegEx.new()regex.compile("\\w-(\\d+)")varresult=regex.search("abc n-0123")ifresult:print(result.get_string())# Would print n-0123

The results of capturing groups() can be retrieved by passing the group number to the various methods inRegExMatch. Group 0 is the default and will always refer to the entire pattern. In the above example, callingresult.get_string(1) would give you0123.

This version of RegEx also supports named capturing groups, and the names can be used to retrieve the results. If two or more groups have the same name, the name would only refer to the first one with a match.

varregex=RegEx.new()regex.compile("d(?<digit>[0-9]+)|x(?<digit>[0-9a-f]+)")varresult=regex.search("the number is x2f")ifresult:print(result.get_string("digit"))# Would print 2f

If you need to process multiple results,search_all() generates a list of all non-overlapping results. This can be combined with afor loop for convenience.

forresultinregex.search_all("d01, d03, d0c, x3f and x42"):print(result.get_string("digit"))# Would print 01 03 0 3f 42

Example: Split a string using a RegEx:

varregex=RegEx.new()regex.compile("\\S+")# Negated whitespace character class.varresults=[]forresultinregex.search_all("One  Two\n\tThree"):results.push_back(result.get_string())# The `results` array now contains "One", "Two", and "Three".

Note: Godot's regex implementation is based on thePCRE2 library. You can view the full pattern referencehere.

Tip: You can useRegexr to test regular expressions online.

Methods

void

clear()

Error

compile(pattern:String, show_error:bool = true)

RegEx

create_from_string(pattern:String, show_error:bool = true)static

int

get_group_count()const

PackedStringArray

get_names()const

String

get_pattern()const

bool

is_valid()const

RegExMatch

search(subject:String, offset:int = 0, end:int = -1)const

Array[RegExMatch]

search_all(subject:String, offset:int = 0, end:int = -1)const

String

sub(subject:String, replacement:String, all:bool = false, offset:int = 0, end:int = -1)const


Method Descriptions

voidclear()🔗

This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.


Errorcompile(pattern:String, show_error:bool = true)🔗

Compiles and assign the search pattern to use. Returns@GlobalScope.OK if the compilation is successful. If compilation fails, returns@GlobalScope.FAILED and whenshow_error istrue, details are printed to standard output.


RegExcreate_from_string(pattern:String, show_error:bool = true)static🔗

Creates and compiles a newRegEx object. See alsocompile().


intget_group_count()const🔗

Returns the number of capturing groups in compiled pattern.


PackedStringArrayget_names()const🔗

Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.


Stringget_pattern()const🔗

Returns the original search pattern that was compiled.


boolis_valid()const🔗

Returns whether this object has a valid search pattern assigned.


RegExMatchsearch(subject:String, offset:int = 0, end:int = -1)const🔗

Searches the text for the compiled pattern. Returns aRegExMatch container of the first matching result if found, otherwisenull.

The region to search within can be specified withoffset andend. This is useful when searching for another match in the samesubject by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor^ is not affected byoffset, and the character beforeoffset will be checked for the word boundary\b.


Array[RegExMatch]search_all(subject:String, offset:int = 0, end:int = -1)const🔗

Searches the text for the compiled pattern. Returns an array ofRegExMatch containers for each non-overlapping result. If no results were found, an empty array is returned instead.

The region to search within can be specified withoffset andend. This is useful when searching for another match in the samesubject by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor^ is not affected byoffset, and the character beforeoffset will be checked for the word boundary\b.


Stringsub(subject:String, replacement:String, all:bool = false, offset:int = 0, end:int = -1)const🔗

Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as$1 and$name are expanded and resolved. By default, only the first instance is replaced, but it can be changed for all instances (global replacement).

The region to search within can be specified withoffset andend. This is useful when searching for another match in the samesubject by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor^ is not affected byoffset, and the character beforeoffset will be checked for the word boundary\b.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.