RegExp for matching three letters, but not text “B

2020-05-24 19:53发布

I have two buttons on form, one of the buttons contain currency code (EUR, USD, GBP,CHF,..) and another one - trade direction (BUY or SELL). And some utility recognize buttons by it's text. To recognize button with currencies, I use Regular expression ":[A-Z]{3}", but it don't work properly when second button contain text "BUY" (regex description returns more than one object).

Question: how can I write pattern for Regular expression, which means: match only when text contain three upper letters, but not text "BUY"?

Thanks!

标签: regex
2条回答
祖国的老花朵
2楼-- · 2020-05-24 20:31
^(?!BUY)[A-Z]{3}$

(?!BUY) is negative lookahead that would fail if it matches the regex BUY

查看更多
爷的心禁止访问
3楼-- · 2020-05-24 20:39

You can use a negative look-behind assertion to verify that the text just matched does not equal BUY.

[A-Z]{3}(?<!BUY)
查看更多
登录 后发表回答