2015-02-21 05:39:41 -05:00
|
|
|
# test named char classes
|
|
|
|
|
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
import re
|
2017-02-14 17:56:22 -05:00
|
|
|
except ImportError:
|
2022-08-18 02:57:45 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2015-02-21 05:39:41 -05:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-21 05:39:41 -05:00
|
|
|
def print_groups(match):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("----")
|
2015-02-21 05:39:41 -05:00
|
|
|
try:
|
|
|
|
i = 0
|
|
|
|
while True:
|
2023-01-17 17:13:35 -05:00
|
|
|
print(match.group(i))
|
2015-02-21 05:39:41 -05:00
|
|
|
i += 1
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
m = re.match(r"\w+", "1234hello567 abc")
|
2015-02-21 05:39:41 -05:00
|
|
|
print_groups(m)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
m = re.match(r"(\w+)\s+(\w+)", "ABC \t1234hello567 abc")
|
2015-02-21 05:39:41 -05:00
|
|
|
print_groups(m)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
m = re.match(r"(\S+)\s+(\D+)", "ABC \thello abc567 abc")
|
2015-02-21 05:39:41 -05:00
|
|
|
print_groups(m)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
m = re.match(r"(([0-9]*)([a-z]*)\d*)", "1234hello567")
|
2015-02-21 05:39:41 -05:00
|
|
|
print_groups(m)
|
2023-01-17 17:13:35 -05:00
|
|
|
|
|
|
|
# named class within a class set
|
|
|
|
print_groups(re.match("([^\s]+)\s*([^\s]+)", "1 23"))
|
|
|
|
print_groups(re.match("([\s\d]+)([\W]+)", "1 2-+="))
|
|
|
|
print_groups(re.match("([\W]+)([^\W]+)([^\S]+)([^\D]+)", " a_1 23"))
|