views:

173

answers:

5

Hi,

Does anybody know how to code a regular expression (for use with some grep like tool) which will search for the word 'vat' but exclude the word 'pri**vat**e'. I've taken over a project which has hundreds of references to VAT (some hard-coded, some not) and as the VAT rate in the UK is changing on January 1 I need to update the project source files with the correct rate. At the moment when I do a global search for the word 'vat' in Visual Studio it also returns lines which contain a private void definition.

In addition to this I need to search for '15' (the current vat rate) but I would like to exclude any lines that have any number before or after the '15'. For example, 0.15 or 15 or 15% would be true, 015 or 150 or 15a would be false.

As you can imagine this is no trivial task to do manually so any help would be much appreciated.

Thanks,

Tim

+5  A: 
/\bVAT\b/

/\b(0\.)?15%?\b/

The last allows things like "0.15%", but those should be few enough to filter out later. Regex isn't the best tool for this, and what about expressions like "10 + 5"? But if it meets your needs, it's at least easy to use!

Roger Pate
+1 I would link an xkcd cartoon, but this has become so cliché...
Pascal Cuoq
+1  A: 

Yes, you can use a technique known as negative 'lookbehind' and 'lookahead', which will match some things not followed by other things.

Here's an overview:

http://www.regular-expressions.info/lookaround.html

Also, Jeff Atwood has a blog posting about it that may be of some small value:

http://www.codinghorror.com/blog/archives/000425.html

The Matt
+1  A: 

Using Visual Studio (as I read you had done beforehand) you can use:

For the vat

~(pri)vat~(e) (This will also exclude 'vate' so just take off the ~(e) if desired)

And for the 15

[^0-9a-z]15[^0-9a-z]

The regular expression syntax VS uses for Find and Replace is not like your standard regex, not sure why MS went that way, but they did.

David
Thanks, this is exactly what I was looking for!!
tt83
A: 

Couldn't you search for " vat " (space vat space)?

Beaner
Unforunately that won't work in this case as I'm trying to search for variable definatitions which could be 'vat_rate' for example.
tt83
A: 

Thanks for all the answers, they've all been very useful!

tt83