Title here
Summary here
# GDScript offers built-in support for regular expressions.
# Here are some examples of common regexp-related tasks in GDScript.
extends Node
func _ready():
# This tests whether a pattern matches a string.
var regex = RegEx.new()
regex.compile("p([a-z]+)ch")
var result = regex.search("peach")
print(result != null)
# Many methods are available on the RegEx object. Here's
# a match test like we saw earlier.
print(regex.search("peach") != null)
# This finds the match for the regexp.
result = regex.search("peach punch")
if result:
print(result.get_string())
# This also finds the first match but returns the
# start and end indexes for the match instead of the
# matching text.
result = regex.search("peach punch")
if result:
print("idx:", [result.get_start(), result.get_end()])
# The captured groups include information about
# both the whole-pattern matches and the submatches
# within those matches. For example this will return
# information for both p([a-z]+)ch and ([a-z]+).
result = regex.search("peach punch")
if result:
print(result.get_strings())
# Similarly this will return information about the
# indexes of matches and submatches.
result = regex.search("peach punch")
if result:
print([result.get_start(), result.get_end(),
result.get_start(1), result.get_end(1)])
# To find all matches for a regexp, we can use search_all.
var results = regex.search_all("peach punch pinch")
var matches = []
for res in results:
matches.append(res.get_string())
print(matches)
# These search_all results are available for the other
# functions we saw above as well.
results = regex.search_all("peach punch pinch")
var all_indices = []
for res in results:
all_indices.append([res.get_start(), res.get_end(),
res.get_start(1), res.get_end(1)])
print("all:", all_indices)
# The regexp package can also be used to replace
# subsets of strings with other values.
var replaced = regex.sub("a peach", "<fruit>", true)
print(replaced)
# The sub_func variant allows you to transform matched
# text with a given function.
func to_upper(result: RegExMatch) -> String:
return result.get_string().to_upper()
replaced = regex.sub("a peach", to_upper)
print(replaced)
# Output:
# true
# true
# peach
# idx: [0, 5]
# [peach, ea]
# [0, 5, 1, 3]
# [peach, punch, pinch]
# all: [[0, 5, 1, 3], [6, 11, 7, 9], [12, 17, 13, 15]]
# a <fruit>
# a PEACH
# For a complete reference on GDScript regular expressions,
# check the RegEx class documentation in the Godot docs.
This GDScript code demonstrates the use of regular expressions, which is similar to Go’s regexp package. Here are some key differences and notes:
RegEx
class for regular expressions.compile
method is used to create a regex pattern.search
method is used for finding matches, similar to Go’s FindString
and related methods.search_all
method is used to find all matches, similar to Go’s FindAllString
.sub
for replacements, which is similar to Go’s ReplaceAllString
.sub
, similar to Go’s ReplaceAllFunc
.The structure and explanation have been maintained as much as possible, with adaptations made for GDScript’s syntax and features. This example should provide a good overview of working with regular expressions in GDScript.