import re
import sys
if sys.version_info < (3, 7):
Pattern = type(re.compile(""))
else:
Pattern = re.Pattern
[docs]
def isregex_expr(expr):
"""
Returns ``True`` is the given expression value is a regular expression
like string with prefix ``re/`` and suffix ``/``, otherwise ``False``.
Arguments:
expr (mixed): expression value to test.
Returns:
bool
"""
if not isinstance(expr, str):
return False
return all([len(expr) > 3, expr.startswith("re/"), expr.endswith("/")])
[docs]
def isregex(value):
"""
Returns ``True`` if the input argument object is a native
regular expression object, otherwise ``False``.
Arguments:
value (mixed): input value to test.
Returns:
bool
"""
if not value:
return False
return any((isregex_expr(value), isinstance(value, Pattern)))
[docs]
def strip_regex(expr):
"""
Strips regular expression notation syntax characters from the given
string expression.
Arguments:
expr (str): regular expression expression to strip
Returns:
str
"""
return expr[3:-1] if isregex_expr(expr) else expr