RegEx

Inherits: Reference < Object

使用正则表达式搜索文本的类。

描述

A regular expression (or regex) is a compact language that can be used to recognise strings that follow a specific pattern, such as URLs, email addresses, complete sentences, etc. For instance, 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 before it can be used.

  1. var regex = RegEx.new()
  2. regex.compile("\\w-(\\d+)")

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 "(?:\\.|[^"])*".

Using 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 RegExMatch.get_string and RegExMatch.get_start.

  1. var regex = RegEx.new()
  2. regex.compile("\\w-(\\d+)")
  3. var result = regex.search("abc n-0123")
  4. if result:
  5. print(result.get_string()) # Would print n-0123

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.

  1. var regex = RegEx.new()
  2. regex.compile("d(?<digit>[0-9]+)|x(?<digit>[0-9a-f]+)")
  3. var result = regex.search("the number is x2f")
  4. if result:
  5. print(result.get_string("digit")) # Would print 2f

If you need to process multiple results, search_all generates a list of all non-overlapping results. This can be combined with a for loop for convenience.

  1. for result in regex.search_all("d01, d03, d0c, x3f and x42"):
  2. print(result.get_string("digit"))
  3. # Would print 01 03 0 3f 42

Example of splitting a string using a RegEx:

  1. var regex = RegEx.new()
  2. regex.compile("\\S+") # Negated whitespace character class.
  3. var results = []
  4. for result in regex.search_all("One Two \n\tThree"):
  5. results.push_back(result.get_string())
  6. # The `results` array now contains "One", "Two", "Three".

Note: Godot’s regex implementation is based on the PCRE2 library. You can view the full pattern reference here.

Tip: You can use Regexr to test regular expressions online.

方法

void

clear ( )

Error

compile ( String pattern )

int

get_group_count ( ) const

Array

get_names ( ) const

String

get_pattern ( ) const

bool

is_valid ( ) const

RegExMatch

search ( String subject, int offset=0, int end=-1 ) const

Array

search_all ( String subject, int offset=0, int end=-1 ) const

String

sub ( String subject, String replacement, bool all=false, int offset=0, int end=-1 ) const

方法说明

  • void clear ( )

这个方法重置了对象的状态,就像它是新创建的一样。也就是说,它取消了这个对象的正则表达式的赋值。


编译并指定要使用的搜索模式。如果编译成功,返回@GlobalScope.OK。如果遇到错误,细节将被打印到标准输出,并返回一个错误。


  • int get_group_count ( ) const

返回编译模式中捕获组的数量。


  • Array get_names ( ) const

返回一个数组,该数组是编译模式中命名的捕获组的名称。它们是按外观排序的。


返回被编译的原始搜索模式。


  • bool is_valid ( ) const

返回此对象是否分配了有效的搜索模式。


在文本中搜索编译后的模式。如果找到,返回第一个匹配结果的RegExMatch容器,否则返回null。可以指定要搜索的区域,而不需要修改开始和结束锚点的位置。


在文本中搜索编译过的模式。为每个不重叠的结果返回一个RegExMatch容器数组。如果没有发现任何结果,则返回一个空数组。可以指定要搜索的区域,而不需要修改开始和结束锚点的位置。


搜索文本中的编译模式,并将其替换为指定的字符串。诸如$1$name等转义和反向引用会被展开和解决。默认情况下,只有第一个实例被替换,但可以对所有实例进行修改(全局替换)。可以指定要搜索的区域,而不需要修改开始和结束锚的位置。