2018-05-23 23:08:51 -04:00
|
|
|
try:
|
|
|
|
import ure as re
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import re
|
|
|
|
except ImportError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("SKIP")
|
2018-05-23 23:08:51 -04:00
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
try:
|
|
|
|
re.sub
|
|
|
|
except AttributeError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("SKIP")
|
2018-05-23 23:08:51 -04:00
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
def multiply(m):
|
|
|
|
return str(int(m.group(0)) * 2)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-05-23 23:08:51 -04:00
|
|
|
print(re.sub("\d+", multiply, "10 20 30 40 50"))
|
|
|
|
|
|
|
|
print(re.sub("\d+", lambda m: str(int(m.group(0)) // 2), "10 20 30 40 50"))
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-05-23 23:08:51 -04:00
|
|
|
def A():
|
|
|
|
return "A"
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
print(re.sub("a", A(), "aBCBABCDabcda."))
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
print(
|
|
|
|
re.sub(
|
2021-03-15 09:57:36 -04:00
|
|
|
r"def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):",
|
|
|
|
"static PyObject*\npy_\\1(void){\n return;\n}\n",
|
|
|
|
"\n\ndef myfunc():\n\ndef myfunc1():\n\ndef myfunc2():",
|
2018-05-23 23:08:51 -04:00
|
|
|
)
|
2020-05-30 05:44:13 -04:00
|
|
|
)
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
print(
|
2021-03-15 09:57:36 -04:00
|
|
|
re.compile("(calzino) (blu|bianco|verde) e (scarpa) (blu|bianco|verde)").sub(
|
|
|
|
r"\g<1> colore \2 con \g<3> colore \4? ...", "calzino blu e scarpa verde"
|
2018-05-23 23:08:51 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# no matches at all
|
2021-03-15 09:57:36 -04:00
|
|
|
print(re.sub("a", "b", "c"))
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
# with maximum substitution count specified
|
2021-03-15 09:57:36 -04:00
|
|
|
print(re.sub("a", "b", "1a2a3a", 2))
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
# invalid group
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
re.sub("(a)", "b\\2", "a")
|
2018-05-23 23:08:51 -04:00
|
|
|
except:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("invalid group")
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
# invalid group with very large number (to test overflow in uPy)
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
re.sub("(a)", "b\\199999999999999999999999999999999999999", "a")
|
2018-05-23 23:08:51 -04:00
|
|
|
except:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("invalid group")
|