from re_test_patterns_groups import test_patterns """ (a)+ 与 (a+) 对于 aaaa 来说是不一样的: (a)+, groups() -> a, (a+), groups() -> aaaa """ test_patterns( 'abbaabbba', [ (r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), # 没有匹配则为None,而不是为空字符串 (r'a((a|b)+)', 'a then seq. of [ab]') ], )
def main(): test_patterns("abbaabbba", [(r"a((a*)(b*))", "a followed by 0-n a and 0-n b")])
#!/usr/bin/env python # -*- coding: UTF-8 -*- from re_test_patterns_groups import test_patterns test_patterns("abbaaabbbbaaaaa", [r"a((a+)|(b+))", r"a((?:a+)|(?:b+))"]) # forma capturing # forma non-capturing
from re_test_patterns_groups import test_patterns test_patterns('abbaabbba', [(r'a((a*)(b*))', 'a fllowed by 0-n a and 0-n b')])
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Nested groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaaabbbbaaaaa', [ r'a((a*)(b*))', # 'a' followed by 0-n 'a' and 0-n 'b' ])
""" #end_pymotw_header import re from re_test_patterns_groups import test_patterns # 'a((a|b)+)' (a then seq. of [ab]) # # 'abbaabbba' # 'abbaabbba' ('bbaabbba', 'a') # the outer group is `bbaabbba`, then find (a|b) - the first inner group in it, so it matches single a or b, whichever is the LAST # character in the string. Only the last match is accessible test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), (r'a((a|b)+)', 'a then seq. of [ab]'), (r'a([ab]+)', 'a then seq. of [ab]'),], ) print('------------------') # the group is the last character matched in the pattern test_patterns( 'bcaabc', [(r'(a|b|c)+', 'seq. of [ab]'), (r'([abc])+', 'seq. of [ab]'),] ) # https://docs.python.org/3/library/re.html # If a group matches multiple times, only the last match is accessible:
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Matching alternative groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns('abbaaabbbbaaaaa', [r'a((a+)|(b+))', # 'a' followed by a sequence of 'a' or sequence of 'b' r'a((a|b)+)', # 'a' followed by a sequence of 'a' or 'b' ])
# re_groups_nested.py from re_test_patterns_groups import test_patterns; test_patterns( 'abbaabbba', [(r'a((a*)(b*))', 'a followed by 0-n a and 0-n b')], ) # re_groups_alternative.py test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), (r'a((a|b)+)', 'a then seq. of [ab]')], ) # re_groups_noncapturing.py test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'capturing form'), (r'a((?:a+)|(?:b+))', 'noncapturing')], )
# re_groups_noncapturing.py from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'forma catturante'), (r'a((?:a+)|(?:b+))', 'non catturante')], )
#!/usr/bin/env python3 # encoding: utf-8 from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'capturing form'), (r'a((?:a+)|(?:b+))', 'noncapturing')], )
# re_groups_alternative.py from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'a poi sequenza di a o sequenza di b'), (r'a((a|b)+)', 'a poi sequenza di [ab]')], )
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Non-capturing groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaaabbbbaaaaa', [ r'a((a+)|(b+))', # capturing form r'a((?:a+)|(?:b+))', # non-capturing ])
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Non-capturing groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns('abbaaabbbbaaaaa', [(r'a((a+)|(b+))', "capturing form"), (r'a((?:a+)|(?:b+))', "non-capturing"), ])
# re_groups_nested.py from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a*)(b*))', 'a seguita da 0-n a e 0-n b')], )
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Matching alternative groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaaabbbbaaaaa', [ r'a((a+)|(b+))', # 'a' followed by a sequence of 'a' or sequence of 'b' r'a((a|b)+)', # 'a' followed by a sequence of 'a' or 'b' ])
#!/usr/bin/env python # -*- coding: UTF-8 -*- from re_test_patterns_groups import test_patterns test_patterns('abbaaabbbbaaaaa', [r'a((a+)|(b+))', # 'a' seguito da una sequanza di 'a' o da una sequenza di 'b' r'a((a|b)+)', # 'a' seguito da una sequenza di 'a' o 'b' ])
def main(): test_patterns("abbaabbba", [(r"a((a+)|(b+))", "capturing form"), (r"a((?:a+)|(?:b+))", "noncapturing")])
#!/usr/bin/env python3 # encoding: utf-8 from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), (r'a((a|b)+)', 'a then seq. of [ab]')], )
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Non-capturing groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns('abbaaabbbbaaaaa', [ (r'a((a+)|(b+))', "capturing form"), (r'a((?:a+)|(?:b+))', "non-capturing"), ])
# re_groups_nested.py from re_test_patterns_groups import test_patterns test_patterns("abbaabbba", [(r"a((a*)(b*))", "a folloewd by 0-n a and 0-n b")])
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Matching alternative groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [ (r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), (r'a((a|b)+)', 'a then seq. of [ab]'), ])
#!/usr/bin/env python3 # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Noncapturing groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a+)|(b+))', 'capturing form'), (r'a((?:a+)|(?:b+))', 'noncapturing')], )
# re_groups_alternative.py from re_test_patterns_groups import test_patterns test_patterns("abbaabbba", [(r"a((a+)|(b+))", "a then seq. of a or ser. of b"), (r"a((a|b)+)", "a then seq. of [ab]")])
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Nested groups """ # end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns("abbaaabbbbaaaaa", [r"a((a*)(b*))"]) # 'a' followed by 0-n 'a' and 0-n 'b'
#!/usr/bin/env python # -*- coding: UTF-8 -*- from re_test_patterns_groups import test_patterns test_patterns('abbaaabbbbaaaaa', [r'a((a*)(b*))', # 'a' seguito da 0-n 'a' e 0-n 'b' ])
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Nested groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [ (r'a((a*)(b*))', 'a followed by 0-n a and 0-n b'), ])
#!/usr/bin/env python3 # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Nested groups """ #end_pymotw_header from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', # if nested groups, starting from most outer group, then inner groups left to right [(r'a((a*)(b*))', 'a followed by 0-n a and 0-n b')], )