tags:

views:

54

answers:

2

Hi all!

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!

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

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

Amarghosh
Yes, it's works :) Thank you for quick answer!
Vitaliy
+1  A: 

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

[A-Z]{3}(?<!BUY)
Daniel Brückner
the look ahead version is more widely supported in different regex implementations, and has better performance (I think).
Jens