Vim does not respect certain regex character classes during substitutions

By Druss , 1 June, 2013

Just now while attempting to perform a substitution in GVim, I found that the regex was not working as expected. Apparently, instead of matching all non-whitespace characters denoted by the shorthand class \S, the engine was matching the letter S instead, which was odd. It turns out that, as usual, I was the one at fault. What I was doing wrong was attempting to use the character class within [] groups. Thinking about it further, it is reasonably redundant to nest one class within another and therefore, understandable.

To conclude, using something like [\S] will not match non-whitespace characters. For that to happen, just use \S. For cases where you want to group multiple character classes together, use explicit [] groups rather than shorthand notation like \S or \w.

Tags

Tags Old

All times are UTC. All content licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.