regex negation question
Does anybody know how to code a regular expression (for use w开发者_开发知识库ith some grep like tool) which will search for the word 'vat' but exclude the word 'private'. 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
/\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!
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.
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
Couldn't you search for " vat " (space vat space)?
精彩评论