From 5f2b6bd476327d2319f1066ab002deec586df8ec Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 29 Jul 2020 12:12:01 +0200 Subject: Document how to perform advanced string splitting using RegEx This closes https://github.com/godotengine/godot-docs/issues/3607. --- doc/classes/String.xml | 5 +++-- modules/regex/doc_classes/RegEx.xml | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 7e55f8bd9a..ded64761d0 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -895,8 +895,8 @@ - Splits the string by a [code]delimiter[/code] string and returns an array of the substrings. - If [code]maxsplit[/code] is specified, it defines the number of splits to do from the left up to [code]maxsplit[/code]. The default value of 0 means that all items are split. + Splits the string by a [code]delimiter[/code] string and returns an array of the substrings. The [code]delimiter[/code] can be of any length. + If [code]maxsplit[/code] is specified, it defines the number of splits to do from the left up to [code]maxsplit[/code]. The default value of [code]0[/code] means that all items are split. Example: [codeblock] var some_string = "One,Two,Three,Four" @@ -905,6 +905,7 @@ print(some_array[0]) # Prints "One" print(some_array[1]) # Prints "Two,Three,Four" [/codeblock] + If you need to split strings with more complex rules, use the [RegEx] class instead. 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. -- cgit v1.2.3