Need a regex to exclude certain strings

后端 未结 6 1426
无人及你
无人及你 2021-01-02 15:18

I\'m trying to get a regex that will match:

somefile_1.txt
somefile_2.txt
somefile_{anything}.txt

but not match:

somefile_1         


        
6条回答
  •  Happy的楠姐
    2021-01-02 15:40

    Some regex libraries allow lookahead:

    somefile(?!16\.txt$).*?\.txt
    

    Otherwise, you can still use multiple character classes:

    somefile([^1].|1[^6]|.|.{3,})\.txt
    

    or, to achieve maximum portability:

    somefile([^1].|1[^6]|.|....*)\.txt
    

    [^(16)] means: Match any character but braces, 1, and 6.

提交回复
热议问题