ure-simple regular expression
This module implements a subset of the corresponding CPython module, as described below. For more information, please refer to the original CPython documentation: re.
This module implements regular expression operations. The supported regular expression syntax is a subset of the CPython
re module (actually a subset of POSIX extended regular expressions).
Supported operators and special sequences
.: matches any character.
[...]: Match character set. Single characters and ranges are supported, including negative sets (eg
^: Match the beginning of the string.
$: match the end of the string.
?: Match zero or one of the previous subpatterns.
*: Match zero or more of the previous subpattern.
+: Match one or more of the previous subpatterns.
??: The non-greedy version of
?, matches zero or one, and the preference is zero.
*?: A non-greedy version of
*, matching zero or more, preferring the shortest match.
+?: Non-greedy "+" version, matching one or more, first matching the shortest.
|: Match the left or right sub-pattern of this operator.
(...): grouping. Each group is capturing (the substring it captures can be accessed using the `match.group() method).
\d: match numbers. Equivalent to
\D: match non-digits. Equivalent to
\S: matches whitespace. Equivalent to
\w: match "word characters" (ASCII only). Equivalent to
\W: match non-"word characters" (ASCII only). Equivalent to
\: escape character. Except for those listed above, any other characters after the backslash are literal. For example,
\*is equivalent to the literal
*(not considered as the
*operator). Please note that
\n,, etc. are not specially processed, and are equivalent to the text letters
n, etc. Therefore, it is not recommended to use raw Python strings (
r"") for regular expressions. For example,
r"\r \n"is equivalent to
"rn"when used as a regular expression. To match the characters of CR followed by LF, use
- Repeated calculation (
- Named group (
- Non-capturing group (
- More advanced assertions (
- Escaping special characters like
\n-use Python's own escape
import ure # As ure doesn't support escapes itself, use of r"" strings is not # recommended. regex = ure.compile("[\r\n]") regex.split("line1\rline2\nline3\r\n") # Result: # ['line1','line2','line3','','']
Compile the regular expression and return the regex object.
Compile regex_str and match the string. The match always starts from the starting position in the string.
Compile regex_str and search for it in the string. Unlike
match, this will search the string to match the first position of the regular expression (if the regular expression is anchored, it can still be 0).
ure.sub(regex_str, replace, string, count=0, flags=0)
Compile regex_str and search for it in the string, replace all matches with replace, and return the new string.
replace can be a string or a function. If it is a string, then escape sequences of the form
\g<number> can be used to expand to the corresponding group (or an empty string that does not match the group). If replace is a function, then it must take one parameter (match) and should return a replacement string.
If count is specified and non-zero, then the replacement will stop after many replacements. The flags parameter is ignored.
Note: The availability of this function depends on the
The tag value displays debugging information about the compiled expression. (Availability depends on the
MicroPython port implementation.)
The compiled regular expression. Use
ure.compile() to create an instance of this class.
regex.match(string) regex.search(string) regex.sub(replace, string, count=0, flags=0)
Similar to the module-level functions
sub(). If the same regular expression is applied to multiple strings, the usage method will be more efficient.
Use regular expressions to split the string. If max_split is given, specify the maximum number of splits to perform. Returns a list of strings (if specified, there can be at most max_split + 1 elements).
Match the objects returned by the
search() methods and pass them to the substitution function in sub().
Returns the matching (sub)string. The index of the entire match is 0, and the index of each capture group is 1 and higher. Only number groups are supported.
Return a tuple containing all substrings of the matched group.
Note: The availability of this method depends on the
MicroPython port implementation.
Returns the index in the original string of the beginning or end of the matched substring group. The index defaults to the entire group, otherwise a group will be selected.
Note: The availability of these methods depends on the
MicroPython port implementation.
Return the 2-tuple
Note: The availability of this method depends on the implementation of
- Supported operators and special sequences
- Unsupported expression
- Regex Object
- Match Object