/ 猿问

# 不以给定后缀结尾的字符串的正则表达式

2019-09-24 15:01:57

b

ab

1

a

ba

.*(?:(?!ab).).\$

## 3 回答

.*(?<!a)\$

(?<!a)是断言式的后置断言，可确保在字符串（或带m修饰符的行）结束之前没有字符“ a”。

.*(?<!ab)\$

.*[^a]\$

.*[^a][^b]\$

HUWWW

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n'

jd8a

8\$fb

q(c

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^a]\$"

8\$fb

q(c

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^b]\$"

jd8a

q(c

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^c]\$"

jd8a

8\$fb

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^a][^b]\$"

jd8a

q(c

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^a][^c]\$"

jd8a

8\$fb

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^a^b]\$"

q(c

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^a^c]\$"

8\$fb

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^b^c]\$"

jd8a

balter@spectre3:~\$ printf 'jd8a\n8\$fb\nq(c\n' | grep ".*[^b^c^a]\$"

FWIW，我在Regex101中发现了相同的结果，我认为这是JavaScript语法。

• 3 回答
• 0 关注
• 1012 浏览

0/150