Regular expression syntax for “match nothing”?

前端 未结 6 917
清酒与你
清酒与你 2021-01-31 00:58

I have a python template engine that heavily uses regexp. It uses concatenation like:

re.compile( regexp1 + \"|\" + regexp2 + \"*|\" + regexp3 + \"+\" )
<         


        
6条回答
  •  旧时难觅i
    2021-01-31 01:24

    This shouldn't match anything:

    re.compile('$^')
    

    So if you replace regexp1, regexp2 and regexp3 with '$^' it will be impossible to find a match. Unless you are using the multi line mode.


    After some tests I found a better solution

    re.compile('a^')
    

    It is impossible to match and will fail earlier than the previous solution. You can replace a with any other character and it will always be impossible to match

提交回复
热议问题