class_regex.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/modules/regex/doc_classes/RegEx.xml.
  6. .. _class_RegEx:
  7. RegEx
  8. =====
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :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 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.
  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 ``"(?:\\.|[^"])*"``. In GDScript, you can also use raw string literals (r-strings). For example, ``compile(r'"(?:\\.|[^"])*"')`` would be read the same.
  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:** Split 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", and "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>`\ (\ pattern\: :ref:`String<class_String>`, show_error\: :ref:`bool<class_bool>` = true\ ) |
  59. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`RegEx<class_RegEx>` | :ref:`create_from_string<class_RegEx_method_create_from_string>`\ (\ pattern\: :ref:`String<class_String>`, show_error\: :ref:`bool<class_bool>` = true\ ) |static| |
  61. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`int<class_int>` | :ref:`get_group_count<class_RegEx_method_get_group_count>`\ (\ ) |const| |
  63. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_names<class_RegEx_method_get_names>`\ (\ ) |const| |
  65. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`String<class_String>` | :ref:`get_pattern<class_RegEx_method_get_pattern>`\ (\ ) |const| |
  67. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`is_valid<class_RegEx_method_is_valid>`\ (\ ) |const| |
  69. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`RegExMatch<class_RegExMatch>` | :ref:`search<class_RegEx_method_search>`\ (\ subject\: :ref:`String<class_String>`, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| |
  71. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`Array<class_Array>`\[:ref:`RegExMatch<class_RegExMatch>`\] | :ref:`search_all<class_RegEx_method_search_all>`\ (\ subject\: :ref:`String<class_String>`, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| |
  73. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`String<class_String>` | :ref:`sub<class_RegEx_method_sub>`\ (\ subject\: :ref:`String<class_String>`, replacement\: :ref:`String<class_String>`, all\: :ref:`bool<class_bool>` = false, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| |
  75. +------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. .. rst-class:: classref-section-separator
  77. ----
  78. .. rst-class:: classref-descriptions-group
  79. Method Descriptions
  80. -------------------
  81. .. _class_RegEx_method_clear:
  82. .. rst-class:: classref-method
  83. |void| **clear**\ (\ ) :ref:`🔗<class_RegEx_method_clear>`
  84. This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.
  85. .. rst-class:: classref-item-separator
  86. ----
  87. .. _class_RegEx_method_compile:
  88. .. rst-class:: classref-method
  89. :ref:`Error<enum_@GlobalScope_Error>` **compile**\ (\ pattern\: :ref:`String<class_String>`, show_error\: :ref:`bool<class_bool>` = true\ ) :ref:`🔗<class_RegEx_method_compile>`
  90. Compiles and assign the search pattern to use. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if the compilation is successful. If compilation fails, returns :ref:`@GlobalScope.FAILED<class_@GlobalScope_constant_FAILED>` and when ``show_error`` is ``true``, details are printed to standard output.
  91. .. rst-class:: classref-item-separator
  92. ----
  93. .. _class_RegEx_method_create_from_string:
  94. .. rst-class:: classref-method
  95. :ref:`RegEx<class_RegEx>` **create_from_string**\ (\ pattern\: :ref:`String<class_String>`, show_error\: :ref:`bool<class_bool>` = true\ ) |static| :ref:`🔗<class_RegEx_method_create_from_string>`
  96. Creates and compiles a new **RegEx** object. See also :ref:`compile<class_RegEx_method_compile>`.
  97. .. rst-class:: classref-item-separator
  98. ----
  99. .. _class_RegEx_method_get_group_count:
  100. .. rst-class:: classref-method
  101. :ref:`int<class_int>` **get_group_count**\ (\ ) |const| :ref:`🔗<class_RegEx_method_get_group_count>`
  102. Returns the number of capturing groups in compiled pattern.
  103. .. rst-class:: classref-item-separator
  104. ----
  105. .. _class_RegEx_method_get_names:
  106. .. rst-class:: classref-method
  107. :ref:`PackedStringArray<class_PackedStringArray>` **get_names**\ (\ ) |const| :ref:`🔗<class_RegEx_method_get_names>`
  108. Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
  109. .. rst-class:: classref-item-separator
  110. ----
  111. .. _class_RegEx_method_get_pattern:
  112. .. rst-class:: classref-method
  113. :ref:`String<class_String>` **get_pattern**\ (\ ) |const| :ref:`🔗<class_RegEx_method_get_pattern>`
  114. Returns the original search pattern that was compiled.
  115. .. rst-class:: classref-item-separator
  116. ----
  117. .. _class_RegEx_method_is_valid:
  118. .. rst-class:: classref-method
  119. :ref:`bool<class_bool>` **is_valid**\ (\ ) |const| :ref:`🔗<class_RegEx_method_is_valid>`
  120. Returns whether this object has a valid search pattern assigned.
  121. .. rst-class:: classref-item-separator
  122. ----
  123. .. _class_RegEx_method_search:
  124. .. rst-class:: classref-method
  125. :ref:`RegExMatch<class_RegExMatch>` **search**\ (\ subject\: :ref:`String<class_String>`, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| :ref:`🔗<class_RegEx_method_search>`
  126. Searches the text for the compiled pattern. Returns a :ref:`RegExMatch<class_RegExMatch>` container of the first matching result if found, otherwise ``null``.
  127. 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. Note that 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``.
  128. .. rst-class:: classref-item-separator
  129. ----
  130. .. _class_RegEx_method_search_all:
  131. .. rst-class:: classref-method
  132. :ref:`Array<class_Array>`\[:ref:`RegExMatch<class_RegExMatch>`\] **search_all**\ (\ subject\: :ref:`String<class_String>`, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| :ref:`🔗<class_RegEx_method_search_all>`
  133. 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.
  134. 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. Note that 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``.
  135. .. rst-class:: classref-item-separator
  136. ----
  137. .. _class_RegEx_method_sub:
  138. .. rst-class:: classref-method
  139. :ref:`String<class_String>` **sub**\ (\ subject\: :ref:`String<class_String>`, replacement\: :ref:`String<class_String>`, all\: :ref:`bool<class_bool>` = false, offset\: :ref:`int<class_int>` = 0, end\: :ref:`int<class_int>` = -1\ ) |const| :ref:`🔗<class_RegEx_method_sub>`
  140. 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).
  141. 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. Note that 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``.
  142. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  143. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  144. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  145. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  146. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  147. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  148. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  149. .. |void| replace:: :abbr:`void (No return value.)`