This is a python port of super-expressive. It allows regular expressions to be expressed in python syntax and idioms. This allows for better editor and version control support, and makes them easier to read and edit.
Notes on implementation choices are in notes.md.
>>> import superexpressive as se
>>> se.to_regex(
... se.START_OF_INPUT,
... se.optional("0x"),
... se.capture(se.range(("a", "f"), ("A", "F"), ("0", "9")), se.exactly(4)),
... se.END_OF_INPUT,
... compile=False
... )
'^(?:0x)?([a-fA-F0-9]{4})$'pip install super-expressive-python
Matches any character except a newline.
Matches a carriage return.
Matches any digit character, is the equivalent of range 0-9
Matches the end of the string or just before the newline at the end of the string.
Matches a newline character.
Matches any non-digit character, this is the inverse of DIGIT
Matches any non-whitespace character, this is the inverse of WHITESPACE_CHAR
Matches the complement of WORD
Matches the empty string, but not at the start or end of a word.
Matches 1 or more (greedy) repetitions of the preceding expression
Non-greedy match for one or more repetitions of the previous expression
Matches 0 or 1 (greedy) of the preceding RE.
Matches the start of the string.
Matches a tab character.
Matches any whitespace character
Matches any alphanumeric character a-z, A-Z, 0-9, or underscore in bytes patterns or string patterns with the ASCII flag. In string patterns without the ASCII flag, it will match the range of Unicode alphanumeric characters (letters plus digits plus underscore).
Matches the empty string, but only at the start or end of a word.
Matches 0 or more (greedy) repetitions of the preceding RE. Greedy means that it will match as many repetitions as possible.
Non-greedy version of the zero or more match
Match any of the given arguments.
>>> import superexpressive as se
>>> se.any_of('A', 'F', 'dkja')
'(?:A|F|dkja)'-
Return type
str
A length 1 item that matches any of the included characters.
>>> import superexpressive as se
>>> se.any_of_chars('A', 'F', 'dkja')
'[AFdkja]'-
Return type
str
A length 1 item that matches anything but the included characters.
>>> import superexpressive as se
>>> se.anything_but_chars('A', 'F', 'dkja')
'[^AFdkja]'-
Return type
str
An item that matches anything but a range of characters.
>>> import superexpressive as se
>>> se.anything_but_range(('A', 'F'))
'[^A-F]'-
Return type
str
Match anything except the provided string.
>>> import superexpressive as se
>>> se.anything_but_string('test')
'(?:[^t][^e][^s][^t])'-
Return type
str
Check, but do not consume, that the regex matches the next part of the string.
>>> import superexpressive as se
>>> se.assert_ahead('test')
'(?=test)'-
Return type
str
Check, that the regex matches the previous part of the string.
>>> import superexpressive as se
>>> se.assert_behind('test')
'(?<=test)'-
Return type
str
Check, but do not consume, that the regex does not match the next part of the string.
>>> import superexpressive as se
>>> se.assert_not_ahead('test')
'(?!test)'-
Return type
str
Check, that the regex does not match the previous part of the string.
>>> import superexpressive as se
>>> se.assert_not_behind('test')
'(?<!test)'-
Return type
str
Match the previous pattern at least length times, greedily.
>>> import superexpressive as se
>>> se.at_least(4)
'{4,}'>>> import superexpressive as se
>>> se.DIGIT + se.at_least(6)
'\\d{6,}'-
Return type
str
Refer to an earlier captured group by 1-based index.
>>> import superexpressive as se
>>> se.back_reference(2)
'\\2'-
Return type
str
Match the previous pattern at between minl and maxl times, greedily.
>>> import superexpressive as se
>>> se.between(4,8)
'{4,8}'>>> import superexpressive as se
>>> se.DIGIT + se.between(6,8)
'\\d{6,8}'-
Return type
str
A group that captures its contents.
>>> import superexpressive as se
>>> se.capture(se.range(("a", "f"), ("0", "9")), 'XXX')
'([a-f0-9]XXX)'-
Return type
str
Match the previous pattern exactly length times.
>>> import superexpressive as se
>>> se.exactly(4)
'{4}'>>> import superexpressive as se
>>> se.DIGIT + se.exactly(6)
'\\d{6}'-
Return type
str
it would be cool to be provide a “labeling” function which could generate the code from a given regex, as part of a debugging suite
-
Return type
str
A group that does not capture its contents.
>>> import superexpressive as se
>>> se.group(se.range(("a", "f"), ("0", "9")), 'XXX')
'(?:[a-f0-9]XXX)'-
Return type
str
Refer to an earlier captured group by name.
>>> import superexpressive as se
>>> se.named_back_reference('test')
'\\k<test>'-
Return type
str
A optional non-capturing group of the items inside.
>>> import superexpressive as se
>>> se.optional(se.DIGIT)
'(?:\\d)?'-
Return type
str
An item that matches a range of characters by ascii code.
>>> import superexpressive as se
>>> se.range(('A', 'F'))
'[A-F]'-
Return type
str
Turn a set of re flags into a string suitable for inclusion in a regex.
>>> import superexpressive as se
>>> se.re_flags_to_string(re.A)
'(?a)'>>> import superexpressive as se
>>> se.re_flags_to_string(re.IGNORECASE | re.LOCALE)
'(?iL)'>>> import superexpressive as se
>>> se.re_flags_to_string()
''-
Return type
str
Turn a collection of strings into a regex.
If compile is True, return a re.compile object. If false, return a regex
string in the python style.
>>> import superexpressive as se
>>> se.to_regex(
... se.START_OF_INPUT,
... se.optional("0x"),
... se.capture(se.range(("a", "f"), ("A", "F"), ("0", "9")), se.exactly(4)),
... se.END_OF_INPUT,
... compile=False
... )
'^(?:0x)?([a-fA-F0-9]{4})$'>>> import superexpressive as se
>>> se.to_regex(compile=False)
''-
Return type
Union[str,compile]