Fix miscellaneous oddities around the class reference (part 5)
This commit is contained in:
@ -17,7 +17,7 @@
|
||||
regex.compile("\\w-(\\d+)")
|
||||
var result = regex.search("abc n-0123")
|
||||
if result:
|
||||
print(result.get_string()) # Would print n-0123
|
||||
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.
|
||||
@ -26,13 +26,13 @@
|
||||
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
|
||||
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"))
|
||||
# Would print 01 03 0 3f 42
|
||||
[/codeblock]
|
||||
[b]Example:[/b] Split a string using a RegEx:
|
||||
[codeblock]
|
||||
@ -41,7 +41,7 @@
|
||||
var results = []
|
||||
for result in regex.search_all("One Two \n\tThree"):
|
||||
results.push_back(result.get_string())
|
||||
# The `results` array now contains "One", "Two", and "Three".
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user