Regex match even number of letters

后端 未结 8 1091
没有蜡笔的小新
没有蜡笔的小新 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.

    0 讨论(0)
  • 2020-12-06 06:26

    A* means match "A" zero or more times.

    For an even number of "A", try: (AA)+

    0 讨论(0)
提交回复
热议问题