VIM Regex Cheatsheet
VIM GLOBAL REPLACEMENT
Delete all lines that do not start with "www" (:v is a negative match):
Delete all blank lines, including lines with only whitespace (:g is a positive match):
Remove the string " - 300k - Cached" from google search results, and matches any size in k:
Replace string "XXX" on every line with the current line number:
For CSV file with 2 values separated by a comma per line. Swap the values on each line.
%s/,$//
VIM SORTING
Sort all lines alphabetically, line by line:
Sort all lines numerically, line by line:
Sort lines 1,2 and 3 numerically, leaving the rest alone:
Sort all lines, remove duplicates:
Sort all lines reverse numerically:
REGEX VALIDATION
Validate string is an email address - This expects that there is only one email in the string, with no leading/trailing whitespace. For more details on email regex, take a look at http://www.regular-expressions.info/email.html
RANDOM EXAMPLES!
Here is a case where I need to transform google maps KML polygon data into a format that will work with Bing maps via the javascript api (version 7). I chose to push each set of polygon points into a javascript array of map location types as shown below.
<KML><Content>-77.315148,39.284253 -77.308349,39.277322 -77.29868,39.276026<Content></KML>
--- end result to be copied/pasted into a .js file
--- note that the Location function/method takes lat/long in reverse order of what KML gives us. v.push(new Microsoft.Maps.Location(39.284253,-77.315148));
v.push(new Microsoft.Maps.Location(39.277322,-77.308349));
v.push(new Microsoft.Maps.Location(39.276026,-77.29868));
%s/<[a-zA-Z\/]\+>//g
--- The dataset I get is all on one line, each record separated by a space. Replace the space with a newline to get records on their own row
%s/ /\r/
--- swap thing before the comma with thing after the comma (unfortunately it leaves an extra comma at the end of the result)
%s/\([^,]\+,\)\(.\+\)/\2,\1/
--- remove that extra comma at the end
%s/,$//
--- prepend each record with the javascript code we want for pushing the row into a JS array. This is specific to this projects js code
%s/^/vertices.push(new Microsoft.Maps.Loaction(/
--- append each record with end parens and semicolon for good JS
%s/$/));/