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 (\/\/\/\)!
- 1Linux Top 3: Alpine Linux 3.4, deepin 15.2 and Linux Lite 3.0
- 2Linux 4.7 Set to Boost Live Patching, Security and Power Management
- 3Linux 4.6 Charred Weasel adds USB 3.1 Support
- 4Linux Top 3: OpenIndiana 2016.04, Ubuntu 16.04 and Debian's New Leader
- 5Linux Top 3: KaOS 2016.04, TurnKey 14.1 and pfSense 2.3