Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp for matching three letters, but not text "BUY"

Tags:

regex

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!

like image 764
vmg Avatar asked Jul 07 '10 13:07

vmg


People also ask

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

What is Alnum in regex?

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters.

How does regex handle special characters?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does ?! Mean in regex?

It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment). Follow this answer to receive notifications.


1 Answers

^(?!BUY)[A-Z]{3}$ 

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

like image 184
Amarghosh Avatar answered Sep 23 '22 21:09

Amarghosh