ure.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. :mod:`ure` -- simple regular expressions
  2. ========================================
  3. .. module:: ure
  4. :synopsis: regular expressions
  5. |see_cpython_module| :mod:`python:re`.
  6. This module implements regular expression operations. Regular expression
  7. syntax supported is a subset of CPython ``re`` module (and actually is
  8. a subset of POSIX extended regular expressions).
  9. Supported operators are:
  10. ``'.'``
  11. Match any character.
  12. ``'[...]'``
  13. Match set of characters. Individual characters and ranges are supported,
  14. including negated sets (e.g. ``[^a-c]``).
  15. ``'^'``
  16. Match the start of the string.
  17. ``'$'``
  18. Match the end of the string.
  19. ``'?'``
  20. Match zero or one of the previous entity.
  21. ``'*'``
  22. Match zero or more of the previous entity.
  23. ``'+'``
  24. Match one or more of the previous entity.
  25. ``'??'``
  26. ``'*?'``
  27. ``'+?'``
  28. ``'|'``
  29. Match either the LHS or the RHS of this operator.
  30. ``'(...)'``
  31. Grouping. Each group is capturing (a substring it captures can be accessed
  32. with `match.group()` method).
  33. **NOT SUPPORTED**: Counted repetitions (``{m,n}``), more advanced assertions
  34. (``\b``, ``\B``), named groups (``(?P<name>...)``), non-capturing groups
  35. (``(?:...)``), etc.
  36. Functions
  37. ---------
  38. .. function:: compile(regex_str, [flags])
  39. Compile regular expression, return `regex <regex>` object.
  40. .. function:: match(regex_str, string)
  41. Compile *regex_str* and match against *string*. Match always happens
  42. from starting position in a string.
  43. .. function:: search(regex_str, string)
  44. Compile *regex_str* and search it in a *string*. Unlike `match`, this will search
  45. string for first position which matches regex (which still may be
  46. 0 if regex is anchored).
  47. .. function:: sub(regex_str, replace, string, count=0, flags=0)
  48. Compile *regex_str* and search for it in *string*, replacing all matches
  49. with *replace*, and returning the new string.
  50. *replace* can be a string or a function. If it is a string then escape
  51. sequences of the form ``\<number>`` and ``\g<number>`` can be used to
  52. expand to the corresponding group (or an empty string for unmatched groups).
  53. If *replace* is a function then it must take a single argument (the match)
  54. and should return a replacement string.
  55. If *count* is specified and non-zero then substitution will stop after
  56. this many substitutions are made. The *flags* argument is ignored.
  57. Note: availability of this function depends on `MicroPython port`.
  58. .. data:: DEBUG
  59. Flag value, display debug information about compiled expression.
  60. (Availability depends on `MicroPython port`.)
  61. .. _regex:
  62. Regex objects
  63. -------------
  64. Compiled regular expression. Instances of this class are created using
  65. `ure.compile()`.
  66. .. method:: regex.match(string)
  67. regex.search(string)
  68. regex.sub(replace, string, count=0, flags=0)
  69. Similar to the module-level functions :meth:`match`, :meth:`search`
  70. and :meth:`sub`.
  71. Using methods is (much) more efficient if the same regex is applied to
  72. multiple strings.
  73. .. method:: regex.split(string, max_split=-1)
  74. Split a *string* using regex. If *max_split* is given, it specifies
  75. maximum number of splits to perform. Returns list of strings (there
  76. may be up to *max_split+1* elements if it's specified).
  77. Match objects
  78. -------------
  79. Match objects as returned by `match()` and `search()` methods, and passed
  80. to the replacement function in `sub()`.
  81. .. method:: match.group([index])
  82. Return matching (sub)string. *index* is 0 for entire match,
  83. 1 and above for each capturing group. Only numeric groups are supported.
  84. .. method:: match.groups()
  85. Return a tuple containing all the substrings of the groups of the match.
  86. Note: availability of this method depends on `MicroPython port`.
  87. .. method:: match.start([index])
  88. match.end([index])
  89. Return the index in the original string of the start or end of the
  90. substring group that was matched. *index* defaults to the entire
  91. group, otherwise it will select a group.
  92. Note: availability of these methods depends on `MicroPython port`.
  93. .. method:: match.span([index])
  94. Returns the 2-tuple ``(match.start(index), match.end(index))``.
  95. Note: availability of this method depends on `MicroPython port`.