使用順序否定環(huán)視(零寬斷言)
/^(?![\s!@#])[^!@#]*(?![\s!@#]).$/
同理兩頭不為a,中間可以為a
/^(?![a]).*(?![a]).$/
末尾不為.exe
^.*(?!\.exe).{4}$
在線測(cè)試工具
https://regexr.com/
零寬斷言
https://www.cnblogs.com/windtrace/p/8422332.html
零寬斷言概述
(?<=pattern) (?<!pattern) STRING (?=pattern) (?!pattern) :各種斷言出現(xiàn)的相對(duì)位置
?< lookbehind STRING <lookahead :負(fù)向 STRING 正向
= :Positive,肯定,表示匹配
! :Negative,否定,表示不匹配
(?=pattern)
零寬肯定正向預(yù)查(Zero-width positive lookahead),非獲取匹配,從字符串右側(cè)查找,須匹配pattern。例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。預(yù)查不消耗字符,即在一個(gè)匹配發(fā)生后,將繼續(xù)從STRING之后開始下一次匹配的搜索,而不是從pattern之后開始。下同
(?!pattern)
零寬否定正向預(yù)查(Zero-width negative lookahead),非獲取匹配,從字符串右側(cè)查找,須不匹配pattern。例如“Windows(?!95|98|NT|2000)”能匹配“Windows3.1”中的“Windows”,但不能匹配“Windows2000”中的“Windows”。
(?<=pattern)
零寬肯定負(fù)向預(yù)查(Zero-width positive lookbehind),非獲取匹配,從字符串左側(cè)查找,須匹配pattern。。例如,“(?<=95|98|NT|2000)Windows”能匹配“2000Windows”中的“Windows”,但不能匹配“3.1Windows”中的“Windows”。
(?<!pattern)
零寬否定負(fù)向預(yù)查(Zero-width negative lookbehind),非獲取匹配,從字符串左側(cè)查找,須不匹配pattern。例如“(?<!95|98|NT|2000)Windows”能匹配“3.1Windows”中的“Windows”,但不能匹配“2000Windows”中的“Windows”。