initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
131
modules/regex/doc_classes/RegEx.xml
Normal file
131
modules/regex/doc_classes/RegEx.xml
Normal file
@@ -0,0 +1,131 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="RegEx" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
|
||||
<brief_description>
|
||||
Class for searching text for patterns using regular expressions.
|
||||
</brief_description>
|
||||
<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 of [code]ab[0-9][/code] would find any string that is [code]ab[/code] followed by any number from [code]0[/code] to [code]9[/code]. 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 [method compile] before it can be used.
|
||||
[codeblock]
|
||||
var regex = RegEx.new()
|
||||
regex.compile("\\w-(\\d+)")
|
||||
[/codeblock]
|
||||
The search pattern must be escaped first for GDScript before it is escaped for the expression. For example, [code]compile("\\d+")[/code] would be read by RegEx as [code]\d+[/code]. Similarly, [code]compile("\"(?:\\\\.|[^\"])*\"")[/code] would be read as [code]"(?:\\.|[^"])*"[/code]. In GDScript, you can also use raw string literals (r-strings). For example, [code]compile(r'"(?:\\.|[^"])*"')[/code] would be read the same.
|
||||
Using [method search], 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 as [method RegExMatch.get_string] and [method RegExMatch.get_start].
|
||||
[codeblock]
|
||||
var regex = RegEx.new()
|
||||
regex.compile("\\w-(\\d+)")
|
||||
var result = regex.search("abc n-0123")
|
||||
if result:
|
||||
print(result.get_string()) # Prints "n-0123"
|
||||
[/codeblock]
|
||||
The results of capturing groups [code]()[/code] can be retrieved by passing the group number to the various methods in [RegExMatch]. Group 0 is the default and will always refer to the entire pattern. In the above example, calling [code]result.get_string(1)[/code] would give you [code]0123[/code].
|
||||
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.
|
||||
[codeblock]
|
||||
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")) # Prints "2f"
|
||||
[/codeblock]
|
||||
If you need to process multiple results, [method search_all] generates a list of all non-overlapping results. This can be combined with a [code]for[/code] loop for convenience.
|
||||
[codeblock]
|
||||
# Prints "01 03 0 3f 42"
|
||||
for result in regex.search_all("d01, d03, d0c, x3f and x42"):
|
||||
print(result.get_string("digit"))
|
||||
[/codeblock]
|
||||
[b]Example:[/b] Split a string using a RegEx:
|
||||
[codeblock]
|
||||
var regex = RegEx.new()
|
||||
regex.compile("\\S+") # Negated whitespace character class.
|
||||
var results = []
|
||||
for result in regex.search_all("One Two \n\tThree"):
|
||||
results.push_back(result.get_string())
|
||||
print(results) # Prints ["One", "Two", "Three"]
|
||||
[/codeblock]
|
||||
[b]Note:[/b] Godot's regex implementation is based on the [url=https://www.pcre.org/]PCRE2[/url] library. You can view the full pattern reference [url=https://www.pcre.org/current/doc/html/pcre2pattern.html]here[/url].
|
||||
[b]Tip:[/b] You can use [url=https://regexr.com/]Regexr[/url] to test regular expressions online.
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="clear">
|
||||
<return type="void" />
|
||||
<description>
|
||||
This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.
|
||||
</description>
|
||||
</method>
|
||||
<method name="compile">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="pattern" type="String" />
|
||||
<param index="1" name="show_error" type="bool" default="true" />
|
||||
<description>
|
||||
Compiles and assign the search pattern to use. Returns [constant OK] if the compilation is successful. If compilation fails, returns [constant FAILED] and when [param show_error] is [code]true[/code], details are printed to standard output.
|
||||
</description>
|
||||
</method>
|
||||
<method name="create_from_string" qualifiers="static">
|
||||
<return type="RegEx" />
|
||||
<param index="0" name="pattern" type="String" />
|
||||
<param index="1" name="show_error" type="bool" default="true" />
|
||||
<description>
|
||||
Creates and compiles a new [RegEx] object. See also [method compile].
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_group_count" qualifiers="const">
|
||||
<return type="int" />
|
||||
<description>
|
||||
Returns the number of capturing groups in compiled pattern.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_names" qualifiers="const">
|
||||
<return type="PackedStringArray" />
|
||||
<description>
|
||||
Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_pattern" qualifiers="const">
|
||||
<return type="String" />
|
||||
<description>
|
||||
Returns the original search pattern that was compiled.
|
||||
</description>
|
||||
</method>
|
||||
<method name="is_valid" qualifiers="const">
|
||||
<return type="bool" />
|
||||
<description>
|
||||
Returns whether this object has a valid search pattern assigned.
|
||||
</description>
|
||||
</method>
|
||||
<method name="search" qualifiers="const">
|
||||
<return type="RegExMatch" />
|
||||
<param index="0" name="subject" type="String" />
|
||||
<param index="1" name="offset" type="int" default="0" />
|
||||
<param index="2" name="end" type="int" default="-1" />
|
||||
<description>
|
||||
Searches the text for the compiled pattern. Returns a [RegExMatch] container of the first matching result if found, otherwise [code]null[/code].
|
||||
The region to search within can be specified with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] 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 [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
|
||||
</description>
|
||||
</method>
|
||||
<method name="search_all" qualifiers="const">
|
||||
<return type="RegExMatch[]" />
|
||||
<param index="0" name="subject" type="String" />
|
||||
<param index="1" name="offset" type="int" default="0" />
|
||||
<param index="2" name="end" type="int" default="-1" />
|
||||
<description>
|
||||
Searches the text for the compiled pattern. Returns an array of [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 with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] 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 [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
|
||||
</description>
|
||||
</method>
|
||||
<method name="sub" qualifiers="const">
|
||||
<return type="String" />
|
||||
<param index="0" name="subject" type="String" />
|
||||
<param index="1" name="replacement" type="String" />
|
||||
<param index="2" name="all" type="bool" default="false" />
|
||||
<param index="3" name="offset" type="int" default="0" />
|
||||
<param index="4" name="end" type="int" default="-1" />
|
||||
<description>
|
||||
Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as [code]$1[/code] and [code]$name[/code] 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 with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] 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 [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
</class>
|
54
modules/regex/doc_classes/RegExMatch.xml
Normal file
54
modules/regex/doc_classes/RegExMatch.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="RegExMatch" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
|
||||
<brief_description>
|
||||
Contains the results of a [RegEx] search.
|
||||
</brief_description>
|
||||
<description>
|
||||
Contains the results of a single [RegEx] match returned by [method RegEx.search] and [method RegEx.search_all]. It can be used to find the position and range of the match and its capturing groups, and it can extract its substring for you.
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="get_end" qualifiers="const">
|
||||
<return type="int" />
|
||||
<param index="0" name="name" type="Variant" default="0" />
|
||||
<description>
|
||||
Returns the end position of the match within the source string. The end position of capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern.
|
||||
Returns -1 if the group did not match or doesn't exist.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_group_count" qualifiers="const">
|
||||
<return type="int" />
|
||||
<description>
|
||||
Returns the number of capturing groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_start" qualifiers="const">
|
||||
<return type="int" />
|
||||
<param index="0" name="name" type="Variant" default="0" />
|
||||
<description>
|
||||
Returns the starting position of the match within the source string. The starting position of capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern.
|
||||
Returns -1 if the group did not match or doesn't exist.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_string" qualifiers="const">
|
||||
<return type="String" />
|
||||
<param index="0" name="name" type="Variant" default="0" />
|
||||
<description>
|
||||
Returns the substring of the match from the source string. Capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern.
|
||||
Returns an empty string if the group did not match or doesn't exist.
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
<members>
|
||||
<member name="names" type="Dictionary" setter="" getter="get_names" default="{}">
|
||||
A dictionary of named groups and its corresponding group number. Only groups that were matched are included. If multiple groups have the same name, that name would refer to the first matching one.
|
||||
</member>
|
||||
<member name="strings" type="PackedStringArray" setter="" getter="get_strings" default="PackedStringArray()">
|
||||
An [Array] of the match and its capturing groups.
|
||||
</member>
|
||||
<member name="subject" type="String" setter="" getter="get_subject" default="""">
|
||||
The source string used with the search pattern to find this matching result.
|
||||
</member>
|
||||
</members>
|
||||
</class>
|
Reference in New Issue
Block a user