Advanced Tips for Search-and-Replace in Linux - page 2
Search and Replace Power Tools
Sometimes it can be useful to use a complemented character set. This is like a range, except that what you match is any character except the ones in the range. So [^aieou] would match any character except the five vowels. One use of this might be to find quoted sentences (e.g. "this quoted sentence") in a piece of text. In Vim, use this:
/"[^"]\+"Note that this is just a search, not a search-and-replace, so you just use / at the start of it. In Emacs, use:
C-M-s "[^"]+"(The long version of C-M-s is M-x isearch-forward-regexp). This pattern looks for the " character, followed by at least one of any character except the quote character ([^"]\+, or [^"]+ in Emacs where you don't need to escape the +), followed by a final quote character.
/"[^"]\+[\n]*[^"]\+"To match three-line quotes, you'd need to add the "zero or more newlines plus one or more non-quote characters" part of this pattern another time before the closing quote.
This is just a quick guide to a couple of useful things you can do with regular expressions: the scope is immense. It's well worth spending a little time getting to grips with them, and it can be quite good fun. Have a go yourself, and soon you too will be familiar with leaning toothpick syndrome (\/\/\/\)!
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!