class_regex.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the RegEx.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_RegEx:
  6. RegEx
  7. =====
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. **Category:** Core
  10. Brief Description
  11. -----------------
  12. Class for searching text for patterns using regular expressions.
  13. Methods
  14. -------
  15. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | void | :ref:`clear<class_RegEx_method_clear>` **(** **)** |
  17. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`compile<class_RegEx_method_compile>` **(** :ref:`String<class_String>` pattern **)** |
  19. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`int<class_int>` | :ref:`get_group_count<class_RegEx_method_get_group_count>` **(** **)** const |
  21. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Array<class_Array>` | :ref:`get_names<class_RegEx_method_get_names>` **(** **)** const |
  23. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`String<class_String>` | :ref:`get_pattern<class_RegEx_method_get_pattern>` **(** **)** const |
  25. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`bool<class_bool>` | :ref:`is_valid<class_RegEx_method_is_valid>` **(** **)** const |
  27. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :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 |
  29. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :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 |
  31. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :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 |
  33. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. Description
  35. -----------
  36. Regular Expression (or regex) is a compact programming 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.
  37. 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.
  38. ::
  39. var regex = RegEx.new()
  40. regex.compile("\\w-(\\d+)")
  41. 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 ``"(?:\\.|[^"])*"``
  42. 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 functions such as :ref:`RegExMatch.get_string<class_RegExMatch_method_get_string>` and :ref:`RegExMatch.get_start<class_RegExMatch_method_get_start>`.
  43. ::
  44. var regex = RegEx.new()
  45. regex.compile("\\w-(\\d+)")
  46. var result = regex.search("abc n-0123")
  47. if result:
  48. print(result.get_string()) # Would print n-0123
  49. The results of capturing groups ``()`` can be retrieved by passing the group number to the various functions in :ref:`RegExMatch<class_RegExMatch>`. Group 0 is the default and would always refer to the entire pattern. In the above example, calling ``result.get_string(1)`` would give you ``0123``.
  50. 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.
  51. ::
  52. var regex = RegEx.new()
  53. regex.compile("d(?<digit>[0-9]+)|x(?<digit>[0-9a-f]+)")
  54. var result = regex.search("the number is x2f")
  55. if result:
  56. print(result.get_string("digit")) # Would print 2f
  57. 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.
  58. ::
  59. for result in regex.search_all("d01, d03, d0c, x3f and x42"):
  60. print(result.get_string("digit"))
  61. # Would print 01 03 3f 42
  62. # Note that d0c would not match
  63. Method Descriptions
  64. -------------------
  65. .. _class_RegEx_method_clear:
  66. - void **clear** **(** **)**
  67. This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object.
  68. ----
  69. .. _class_RegEx_method_compile:
  70. - :ref:`Error<enum_@GlobalScope_Error>` **compile** **(** :ref:`String<class_String>` pattern **)**
  71. Compiles and assign the search pattern to use. Returns OK if the compilation is successful. If an error is encountered the details are printed to STDOUT and FAILED is returned.
  72. ----
  73. .. _class_RegEx_method_get_group_count:
  74. - :ref:`int<class_int>` **get_group_count** **(** **)** const
  75. Returns the number of capturing groups in compiled pattern.
  76. ----
  77. .. _class_RegEx_method_get_names:
  78. - :ref:`Array<class_Array>` **get_names** **(** **)** const
  79. Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
  80. ----
  81. .. _class_RegEx_method_get_pattern:
  82. - :ref:`String<class_String>` **get_pattern** **(** **)** const
  83. Returns the original search pattern that was compiled.
  84. ----
  85. .. _class_RegEx_method_is_valid:
  86. - :ref:`bool<class_bool>` **is_valid** **(** **)** const
  87. Returns whether this object has a valid search pattern assigned.
  88. ----
  89. .. _class_RegEx_method_search:
  90. - :ref:`RegExMatch<class_RegExMatch>` **search** **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** const
  91. Searches the text for the compiled pattern. Returns a :ref:`RegExMatch<class_RegExMatch>` container of the first matching result if found, otherwise null. The region to search within can be specified without modifying where the start and end anchor would be.
  92. ----
  93. .. _class_RegEx_method_search_all:
  94. - :ref:`Array<class_Array>` **search_all** **(** :ref:`String<class_String>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** const
  95. 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. The region to search within can be specified without modifying where the start and end anchor would be.
  96. ----
  97. .. _class_RegEx_method_sub:
  98. - :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
  99. 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). The region to search within can be specified without modifying where the start and end anchor would be.