class_regex.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/modules/regex/doc_classes/RegEx.xml.
  6. .. _class_RegEx:
  7. RegEx
  8. =====
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. Class for searching text for patterns using regular expressions.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. To begin, the RegEx object needs to be compiled with the search pattern using :ref:`compile<class_RegEx_method_compile>` before it can be used.
  16. ::
  17. var regex = RegEx.new()
  18. regex.compile("\\w-(\\d+)")
  19. 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 ``"(?:\\.|[^"])*"``.
  20. Using :ref:`search<class_RegEx_method_search>`, you can find the pattern within the given text. If a pattern is found, :ref:`RegExMatch<class_RegExMatch>` is returned and you can retrieve details of the results using methods such as :ref:`RegExMatch.get_string<class_RegExMatch_method_get_string>` and :ref:`RegExMatch.get_start<class_RegExMatch_method_get_start>`.
  21. ::
  22. var regex = RegEx.new()
  23. regex.compile("\\w-(\\d+)")
  24. var result = regex.search("abc n-0123")
  25. if result:
  26. print(result.get_string()) # Would print n-0123
  27. The results of capturing groups ``()`` can be retrieved by passing the group number to the various methods in :ref:`RegExMatch<class_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``.
  28. 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.
  29. ::
  30. var regex = RegEx.new()
  31. regex.compile("d(?<digit>[0-9]+)|x(?<digit>[0-9a-f]+)")
  32. var result = regex.search("the number is x2f")
  33. if result:
  34. print(result.get_string("digit")) # Would print 2f
  35. If you need to process multiple results, :ref:`search_all<class_RegEx_method_search_all>` generates a list of all non-overlapping results. This can be combined with a ``for`` loop for convenience.
  36. ::
  37. for result in regex.search_all("d01, d03, d0c, x3f and x42"):
  38. print(result.get_string("digit"))
  39. # Would print 01 03 0 3f 42
  40. \ **Example of splitting a string using a RegEx:**\
  41. ::
  42. var regex = RegEx.new()
  43. regex.compile("\\S+") # Negated whitespace character class.
  44. var results = []
  45. for result in regex.search_all("One Two \n\tThree"):
  46. results.push_back(result.get_string())
  47. # The `results` array now contains "One", "Two", "Three".
  48. \ **Note:** Godot's regex implementation is based on the `PCRE2 <https://www.pcre.org/>`__ library. You can view the full pattern reference `here <https://www.pcre.org/current/doc/html/pcre2pattern.html>`__.
  49. \ **Tip:** You can use `Regexr <https://regexr.com/>`__ to test regular expressions online.
  50. .. rst-class:: classref-reftable-group
  51. Methods
  52. -------
  53. .. table::
  54. :widths: auto
  55. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`clear<class_RegEx_method_clear>` **(** **)** |
  57. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`compile<class_RegEx_method_compile>` **(** :ref:`String<class_String>` pattern **)** |
  59. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`int<class_int>` | :ref:`get_group_count<class_RegEx_method_get_group_count>` **(** **)** |const| |
  61. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`Array<class_Array>` | :ref:`get_names<class_RegEx_method_get_names>` **(** **)** |const| |
  63. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`String<class_String>` | :ref:`get_pattern<class_RegEx_method_get_pattern>` **(** **)** |const| |
  65. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`bool<class_bool>` | :ref:`is_valid<class_RegEx_method_is_valid>` **(** **)** |const| |
  67. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`RegExMatch<class_RegExMatch>` | :ref:`search<class_RegEx_method_search>` **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const| |
  69. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`Array<class_Array>` | :ref:`search_all<class_RegEx_method_search_all>` **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const| |
  71. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`String<class_String>` | :ref:`sub<class_RegEx_method_sub>` **(** :ref:`String<class_String>` subject, :ref:`String<class_String>` replacement, :ref:`bool<class_bool>` all=false, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const| |
  73. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. .. rst-class:: classref-section-separator
  75. ----
  76. .. rst-class:: classref-descriptions-group
  77. Method Descriptions
  78. -------------------
  79. .. _class_RegEx_method_clear:
  80. .. rst-class:: classref-method
  81. void **clear** **(** **)**
  82. This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.
  83. .. rst-class:: classref-item-separator
  84. ----
  85. .. _class_RegEx_method_compile:
  86. .. rst-class:: classref-method
  87. :ref:`Error<enum_@GlobalScope_Error>` **compile** **(** :ref:`String<class_String>` pattern **)**
  88. Compiles and assign the search pattern to use. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if the compilation is successful. If an error is encountered, details are printed to standard output and an error is returned.
  89. .. rst-class:: classref-item-separator
  90. ----
  91. .. _class_RegEx_method_get_group_count:
  92. .. rst-class:: classref-method
  93. :ref:`int<class_int>` **get_group_count** **(** **)** |const|
  94. Returns the number of capturing groups in compiled pattern.
  95. .. rst-class:: classref-item-separator
  96. ----
  97. .. _class_RegEx_method_get_names:
  98. .. rst-class:: classref-method
  99. :ref:`Array<class_Array>` **get_names** **(** **)** |const|
  100. Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
  101. .. rst-class:: classref-item-separator
  102. ----
  103. .. _class_RegEx_method_get_pattern:
  104. .. rst-class:: classref-method
  105. :ref:`String<class_String>` **get_pattern** **(** **)** |const|
  106. Returns the original search pattern that was compiled.
  107. .. rst-class:: classref-item-separator
  108. ----
  109. .. _class_RegEx_method_is_valid:
  110. .. rst-class:: classref-method
  111. :ref:`bool<class_bool>` **is_valid** **(** **)** |const|
  112. Returns whether this object has a valid search pattern assigned.
  113. .. rst-class:: classref-item-separator
  114. ----
  115. .. _class_RegEx_method_search:
  116. .. rst-class:: classref-method
  117. :ref:`RegExMatch<class_RegExMatch>` **search** **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const|
  118. Searches the text for the compiled pattern. Returns a :ref:`RegExMatch<class_RegExMatch>` container of the first matching result if found, otherwise ``null``.
  119. The region to search within can be specified with ``offset`` and ``end``. This is useful when searching for another match in the same ``subject`` by calling this method again after a previous success. Setting these parameters differs from passing over a shortened string. For example, the start anchor ``^`` is not affected by ``offset``, and the character before ``offset`` will be checked for the word boundary ``\b``.
  120. .. rst-class:: classref-item-separator
  121. ----
  122. .. _class_RegEx_method_search_all:
  123. .. rst-class:: classref-method
  124. :ref:`Array<class_Array>` **search_all** **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const|
  125. Searches the text for the compiled pattern. Returns an array of :ref:`RegExMatch<class_RegExMatch>` containers for each non-overlapping result. If no results were found, an empty array is returned instead.
  126. The region to search within can be specified with ``offset`` and ``end``. This is useful when searching for another match in the same ``subject`` by calling this method again after a previous success. Setting these parameters differs from passing over a shortened string. For example, the start anchor ``^`` is not affected by ``offset``, and the character before ``offset`` will be checked for the word boundary ``\b``.
  127. .. rst-class:: classref-item-separator
  128. ----
  129. .. _class_RegEx_method_sub:
  130. .. rst-class:: classref-method
  131. :ref:`String<class_String>` **sub** **(** :ref:`String<class_String>` subject, :ref:`String<class_String>` replacement, :ref:`bool<class_bool>` all=false, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** |const|
  132. 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).
  133. The region to search within can be specified with ``offset`` and ``end``. This is useful when searching for another match in the same ``subject`` by calling this method again after a previous success. Setting these parameters differs from passing over a shortened string. For example, the start anchor ``^`` is not affected by ``offset``, and the character before ``offset`` will be checked for the word boundary ``\b``.
  134. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  135. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  136. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  137. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`