Regex match even number of letters

后端 未结 8 1103
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 05:35

I need to match an expression in Python with regular expressions that only matches even number of letter occurrences. For example:

AAA        # no match
AA                


        
8条回答
  •  一个人的身影
    2020-12-06 06:25

    Why work so hard coming up with a hard to read pattern? Just search for all occurrences of the pattern and count how many you find.

    len(re.findall("A", "AbcAbcAbcA")) % 2 == 0
    

    That should be instantly understandable by all experienced programmers, whereas a pattern like "(?

    Simple is better.

提交回复
热议问题