Skip to content

Latest commit

 

History

History
144 lines (90 loc) · 10.5 KB

class_regex.rst

File metadata and controls

144 lines (90 loc) · 10.5 KB

RegEx

Inherits: Reference<class_reference> < Object<class_object>

Category: Core

Brief Description

Class for searching text for patterns using regular expressions.

Member Functions

void clear<class_RegEx_clear> ( )
int<class_int> compile<class_RegEx_compile> ( String<class_string> pattern )
int<class_int> get_group_count<class_RegEx_get_group_count> ( ) const
Array<class_array> get_names<class_RegEx_get_names> ( ) const
String<class_string> get_pattern<class_RegEx_get_pattern> ( ) const
bool<class_bool> is_valid<class_RegEx_is_valid> ( ) const
RegExMatch<class_regexmatch> search<class_RegEx_search> ( String<class_string> subject, int<class_int> offset=0, int<class_int> end=-1 ) const
Array<class_array> search_all<class_RegEx_search_all> ( String<class_string> subject, int<class_int> offset=0, int<class_int> end=-1 ) const
String<class_string> sub<class_RegEx_sub> ( String<class_string> subject, String<class_string> replacement, bool<class_bool> all=false, int<class_int> offset=0, int<class_int> end=-1 ) const

Description

Regular Expression (or regex) is a compact programming language that can be used to recognise strings that follow a specific pattern, such as URLs, email addresses, complete sentences, etc. For instance, a regex of ab[0-9] would find any string that is ab followed by any number from 0 to 9. 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 using compile<class_RegEx_compile> before it can be used.

var regex = 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 "(?:\\.|[^"])*"

Using search<class_RegEx_search> you can find the pattern within the given text. If a pattern is found, RegExMatch<class_regexmatch> is returned and you can retrieve details of the results using functions such as RegExMatch.get_string<class_RegExMatch_get_string> and RegExMatch.get_start<class_RegExMatch_get_start>.

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

The results of capturing groups () can be retrieved by passing the group number to the various functions in RegExMatch<class_regexmatch>. Group 0 is the default and would always refer to the entire pattern. In the above example, calling result.get_string(1) would give you 0123.

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.

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

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

for result in regex.search_all("d01, d03, d0c, x3f and x42"):
    print(result.get_string("digit"))
# Would print 01 03 3f 42
# Note that d0c would not match

Member Function Description

  • void clear ( )

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

  • int<class_int> compile ( String<class_string> pattern )

Compiles and assign the search pattern to use. Returns OK if the compilation is successful. If an error is encountered the details are printed to STDOUT and FAILED is returned.

  • int<class_int> get_group_count ( ) const

Returns the number of capturing groups in compiled pattern.

  • Array<class_array> get_names ( ) const

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

  • String<class_string> get_pattern ( ) const

Returns the original search pattern that was compiled.

  • bool<class_bool> is_valid ( ) const

Returns whether this object has a valid search pattern assigned.

Searches the text for the compiled pattern. Returns a RegExMatch<class_regexmatch> container of the first matching result if found, otherwise null. The region to search within can be specified without modifying where the start and end anchor would be.

  • Array<class_array> search_all ( String<class_string> subject, int<class_int> offset=0, int<class_int> end=-1 ) const

Searches the text for the compiled pattern. Returns an array of RegExMatch<class_regexmatch> 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 without modifying where the start and end anchor would be.

  • String<class_string> sub ( String<class_string> subject, String<class_string> replacement, bool<class_bool> all=false, int<class_int> offset=0, int<class_int> end=-1 ) const

Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as \1 and \g<name> 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 without modifying where the start and end anchor would be.