class_regex.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the RegEx.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_RegEx:
  5. RegEx
  6. =====
  7. **Inherits:** :ref:`Reference<class_reference>` **<** :ref:`Object<class_object>`
  8. **Category:** Core
  9. Brief Description
  10. -----------------
  11. Simple regular expression matcher.
  12. Member Functions
  13. ----------------
  14. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | void | :ref:`clear<class_RegEx_clear>` **(** **)** |
  16. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | :ref:`int<class_int>` | :ref:`compile<class_RegEx_compile>` **(** :ref:`String<class_string>` pattern **)** |
  18. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | :ref:`int<class_int>` | :ref:`get_group_count<class_RegEx_get_group_count>` **(** **)** const |
  20. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`Array<class_array>` | :ref:`get_names<class_RegEx_get_names>` **(** **)** const |
  22. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`String<class_string>` | :ref:`get_pattern<class_RegEx_get_pattern>` **(** **)** const |
  24. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`bool<class_bool>` | :ref:`is_valid<class_RegEx_is_valid>` **(** **)** const |
  26. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`RegExMatch<class_regexmatch>` | :ref:`search<class_RegEx_search>` **(** :ref:`String<class_string>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** const |
  28. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`String<class_string>` | :ref:`sub<class_RegEx_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 |
  30. +--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. Description
  32. -----------
  33. Class for finding text patterns in a string using regular expressions. It can not perform replacements. Regular expressions are a way to define patterns of text to be searched. Details on writing patterns are too long to explain here but the Internet is full of tutorials and detailed explanations.
  34. Once created, the RegEx object needs to be compiled with the search pattern before it can be used. The search pattern must be escaped first for gdscript before it is escaped for the expression. For example:
  35. ``var exp = RegEx.new()``
  36. ``exp.compile("\\d+")``
  37. would be read by RegEx as ``\d+``
  38. Similarly:
  39. ``exp.compile("\"(?:\\\\.|[^\"])\*\"")``
  40. would be read as ``"(?:\\.|[^"])\*"``
  41. Currently supported features:
  42. \* Capturing ``()`` and non-capturing ``(?:)`` groups
  43. \* Named capturing groups ``(?P<name>)``
  44. \* Any character ``.``
  45. \* Shorthand character classes ``\w \W \s \S \d \D``
  46. \* User-defined character classes such as ``[A-Za-z]``
  47. \* Simple quantifiers ``?``, ``\*`` and ``+``
  48. \* Range quantifiers ``{x,y}``
  49. \* Lazy (non-greedy) quantifiers ``\*?``
  50. \* Beginning ``^`` and end ``$`` anchors
  51. \* Alternation ``|``
  52. \* Backreferences ``\1``, ``\g{1}``, and ``\g<name>``
  53. \* POSIX character classes ``[[:alnum:]]``
  54. \* Lookahead ``(?=)``, ``(?!)`` and lookbehind ``(?<=)``, ``(?<!)``
  55. \* ASCII ``\xFF`` and Unicode ``\uFFFF`` code points (in a style similar to Python)
  56. \* Word boundaries ``\b``, ``\B``
  57. Member Function Description
  58. ---------------------------
  59. .. _class_RegEx_clear:
  60. - void **clear** **(** **)**
  61. This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object.
  62. .. _class_RegEx_compile:
  63. - :ref:`int<class_int>` **compile** **(** :ref:`String<class_string>` pattern **)**
  64. Compiles and assign the search pattern to use.
  65. .. _class_RegEx_get_group_count:
  66. - :ref:`int<class_int>` **get_group_count** **(** **)** const
  67. Returns the number of numeric capturing groups.
  68. .. _class_RegEx_get_names:
  69. - :ref:`Array<class_array>` **get_names** **(** **)** const
  70. Returns an array of names of named capturing groups.
  71. .. _class_RegEx_get_pattern:
  72. - :ref:`String<class_string>` **get_pattern** **(** **)** const
  73. Returns the search pattern used to compile the code.
  74. .. _class_RegEx_is_valid:
  75. - :ref:`bool<class_bool>` **is_valid** **(** **)** const
  76. Returns whether this object has a valid search pattern assigned.
  77. .. _class_RegEx_search:
  78. - :ref:`RegExMatch<class_regexmatch>` **search** **(** :ref:`String<class_string>` subject, :ref:`int<class_int>` offset=0, :ref:`int<class_int>` end=-1 **)** const
  79. 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.
  80. .. _class_RegEx_sub:
  81. - :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
  82. Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as ``\1`` and ``\g<name>`` 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.