class_regex.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the doc/base/classes.xml source instead.
  3. .. _class_RegEx:
  4. RegEx
  5. =====
  6. **Inherits:** :ref:`Reference<class_reference>` **<** :ref:`Object<class_object>`
  7. **Category:** Core
  8. Brief Description
  9. -----------------
  10. Simple regular expression matcher.
  11. Member Functions
  12. ----------------
  13. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | void | :ref:`clear<class_RegEx_clear>` **(** **)** |
  15. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | :ref:`int<class_int>` | :ref:`compile<class_RegEx_compile>` **(** :ref:`String<class_string>` pattern, :ref:`int<class_int>` capture=9 **)** |
  17. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | :ref:`int<class_int>` | :ref:`find<class_RegEx_find>` **(** :ref:`String<class_string>` text, :ref:`int<class_int>` start=0, :ref:`int<class_int>` end=-1 **)** const |
  19. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`String<class_string>` | :ref:`get_capture<class_RegEx_get_capture>` **(** :ref:`int<class_int>` capture **)** const |
  21. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`int<class_int>` | :ref:`get_capture_count<class_RegEx_get_capture_count>` **(** **)** const |
  23. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`int<class_int>` | :ref:`get_capture_start<class_RegEx_get_capture_start>` **(** :ref:`int<class_int>` capture **)** const |
  25. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`StringArray<class_stringarray>` | :ref:`get_captures<class_RegEx_get_captures>` **(** **)** const |
  27. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`bool<class_bool>` | :ref:`is_valid<class_RegEx_is_valid>` **(** **)** const |
  29. +----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  30. Description
  31. -----------
  32. 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.
  33. Once created, the RegEx object needs to be compiled with the pattern before it can be used. The pattern must be escaped first for gdscript before it is escaped for the expression. For example:
  34. ``var exp = RegEx.new()``
  35. ``exp.compile("\\d+")``
  36. would be read by RegEx as ``\d+``
  37. Similarly:
  38. ``exp.compile("\"(?:\\\\.|[^\"])\*\"")``
  39. would be read as ``"(?:\\.|[^"])\*"``
  40. Currently supported features:
  41. \* Capturing ``()`` and non-capturing ``(?:)`` groups
  42. \* Any character ``.``
  43. \* Shorthand character classes ``\w \W \s \S \d \D``
  44. \* User-defined character classes such as ``[A-Za-z]``
  45. \* Simple quantifiers ``?``, ``\*`` and ``+``
  46. \* Range quantifiers ``{x,y}``
  47. \* Lazy (non-greedy) quantifiers ``\*?``
  48. \* Beginning ``^`` and end ``$`` anchors
  49. \* Alternation ``|``
  50. \* Backreferences ``\1`` and ``\g{1}``
  51. \* POSIX character classes ``[[:alnum:]]``
  52. \* Lookahead ``(?=)``, ``(?!)`` and lookbehind ``(?<=)``, ``(?<!)``
  53. \* ASCII ``\xFF`` and Unicode ``\uFFFF`` code points (in a style similar to Python)
  54. \* Word boundaries ``\b``, ``\B``
  55. Member Function Description
  56. ---------------------------
  57. .. _class_RegEx_clear:
  58. - void **clear** **(** **)**
  59. This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object, and forgets all captures made by the last :ref:`find<class_RegEx_find>`.
  60. .. _class_RegEx_compile:
  61. - :ref:`int<class_int>` **compile** **(** :ref:`String<class_string>` pattern, :ref:`int<class_int>` capture=9 **)**
  62. Compiles and assign the regular expression pattern to use. The limit on the number of capturing groups can be specified or made unlimited if negative.
  63. .. _class_RegEx_find:
  64. - :ref:`int<class_int>` **find** **(** :ref:`String<class_string>` text, :ref:`int<class_int>` start=0, :ref:`int<class_int>` end=-1 **)** const
  65. This method tries to find the pattern within the string, and returns the position where it was found. It also stores any capturing group (see :ref:`get_capture<class_RegEx_get_capture>`) for further retrieval.
  66. .. _class_RegEx_get_capture:
  67. - :ref:`String<class_string>` **get_capture** **(** :ref:`int<class_int>` capture **)** const
  68. Returns a captured group. A captured group is the part of a string that matches a part of the pattern delimited by parentheses (unless they are non-capturing parentheses *(?:)*).
  69. .. _class_RegEx_get_capture_count:
  70. - :ref:`int<class_int>` **get_capture_count** **(** **)** const
  71. Returns the number of capturing groups. A captured group is the part of a string that matches a part of the pattern delimited by parentheses (unless they are non-capturing parentheses *(?:)*).
  72. .. _class_RegEx_get_capture_start:
  73. - :ref:`int<class_int>` **get_capture_start** **(** :ref:`int<class_int>` capture **)** const
  74. .. _class_RegEx_get_captures:
  75. - :ref:`StringArray<class_stringarray>` **get_captures** **(** **)** const
  76. Return a list of all the captures made by the regular expression.
  77. .. _class_RegEx_is_valid:
  78. - :ref:`bool<class_bool>` **is_valid** **(** **)** const
  79. Returns whether this object has a valid regular expression assigned.