RegEx
Class for searching text for patterns using regular expressions.
RegEx.swift:30class RegEx
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 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(pattern:)
before it can be used.
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 "(?:\\.|[^"])*"
. In GDScript, you can also use raw string literals (r-strings). For example, compile(r'"(?:\\.|[^"])*"')
would be read the same.
Using search(subject:offset:end:)
, 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 getString(name:)
and getStart(name:)
.
The results of capturing groups ()
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 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.
If you need to process multiple results, searchAll(subject:offset:end:)
generates a list of all non-overlapping results. This can be combined with a for
loop for convenience.
Example of splitting a string using a RegEx:
Tip: You can use Regexr to test regular expressions online.
Superclasses
class RefCounted
Base class for reference-counted objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
static func createFromString(pattern: String
) -> RegEx? Creates and compiles a new
RegEx
object.class var godotClassName: StringName
Instance members
func clear(
) This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.
func compile(pattern: String
) -> GodotError Compiles and assign the search pattern to use. Returns
ok
if the compilation is successful. If an error is encountered, details are printed to standard output and an error is returned.func getGroupCount(
) -> Int32 Returns the number of capturing groups in compiled pattern.
func getNames(
) -> PackedStringArray Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
func getPattern(
) -> String Returns the original search pattern that was compiled.
func isValid(
) -> Bool Returns whether this object has a valid search pattern assigned.
func search(subject: String, offset: Int32, end: Int32
) -> RegExMatch? Searches the text for the compiled pattern. Returns a
RegExMatch
container of the first matching result if found, otherwisenull
.func searchAll(subject: String, offset: Int32, end: Int32
) -> ObjectCollection<RegExMatch> 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.func sub(subject: String, replacement: String, all: Bool, offset: Int32, end: Int32
) -> String Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as
$1
and$name
are expanded and resolved. By default, only the first instance is replaced, but it can be changed for all instances (global replacement).