summaryrefslogtreecommitdiff
path: root/modules/regex
diff options
context:
space:
mode:
authorHugo Locurcio <hugo.locurcio@hugo.pro>2020-07-29 12:12:01 +0200
committerHugo Locurcio <hugo.locurcio@hugo.pro>2020-07-29 12:12:01 +0200
commit5f2b6bd476327d2319f1066ab002deec586df8ec (patch)
tree10087490a32786a94d3ccd0b8e4c1196105b6894 /modules/regex
parent3890638d84e39a7d5b3f4696f1ca069d34e33964 (diff)
Document how to perform advanced string splitting using RegEx
This closes https://github.com/godotengine/godot-docs/issues/3607.
Diffstat (limited to 'modules/regex')
-rw-r--r--modules/regex/doc_classes/RegEx.xml13
1 files changed, 11 insertions, 2 deletions
diff --git a/modules/regex/doc_classes/RegEx.xml b/modules/regex/doc_classes/RegEx.xml
index c00fa96b2e..312275842a 100644
--- a/modules/regex/doc_classes/RegEx.xml
+++ b/modules/regex/doc_classes/RegEx.xml
@@ -11,7 +11,7 @@
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].
- 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 functions such as [method RegExMatch.get_string] and [method RegExMatch.get_start].
+ 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+)")
@@ -19,7 +19,7 @@
if result:
print(result.get_string()) # Would print n-0123
[/codeblock]
- The results of capturing groups [code]()[/code] can be retrieved by passing the group number to the various functions 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].
+ 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()
@@ -34,6 +34,15 @@
print(result.get_string("digit"))
# Would print 01 03 0 3f 42
[/codeblock]
+ [b]Example of splitting a string using a RegEx:[/b]
+ [codeblock]
+ var regex = RegEx.new()
+ regex.compile("\\S+") # Negated whitespace character class.
+ var results = []
+ for match in regex.search_all("One Two \n\tThree"):
+ results.push_back(match.get_string())
+ # The `results` array now contains "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>