Monday, September 12, 2011

Searching code for a string not in the comments...

Every few months I need to search my code for a string but there are a bunch of references to that code in the comments and I want a regex that will remove those for me. I forget how to do it so every few months I have to figure it out again, well hopefully I will remember when I put it here:


>cat example.txt

token asdf asdf @OK
asdf token asdf @OK
//token @BAD
asdf token asdf // asdf token asdf @OK
// This is a token @BAD
/some/dir/token @OK
aaa token try again @OK
aaa token try again @OK
aaaa token try again @OK

>egrep '^.?([^/][^/])*token' example.txt

token asdf asdf @OK
asdf token asdf @OK
asdf token asdf // asdf token asdf @OK
aaaatoken try again @OK
aaa token try again @OK
aaaa token try again @OK