Archive

Posts Tagged ‘Regular Expression’

Regular Expressions: Searching for words

June 17th, 2009 Russell No comments

I had to do a mass replace to a SQL script that contained dictionary words. Visual Studio’s find & replace didn’t cut it, becuase there were 530,000 lines of SQL.

Instead, trusty Windows Grep (wingrep) came to the rescue. Two regular expression searches (then replace with blank text) only took 5 minutes!

My lines were in this format:
INSERT INTO Dictionary (word) VALUES ('apple')
I made the assumption that all words are in lower case.

Here are the regular expressions I used:

Find lines with words that contain non-alphabetic characters:
^INSERT.*\'[a-z]*[^a-z]+[a-z]*\’\)$

Find lines with words that are of length 1-3 characters:
^INSERT.*\'[a-z][a-z]?[a-z]?\’\)$

Normally in regular expressions I would have done this instead:
^INSERT.*\'[a-z]{1,3}\’\)$

But wingrep doesn’t appear to support {N, L} functionality.