Given the following 3 lines

"unit": "mm"
"unit": "s"
"unit": "%"

If I do a regex search /"unit": "[^sm]", it would return "unit": "%".

However, for /"unit": "[^s%]" or /"unit": "[^s\%]", it didn't return "unit": "mm".

Why is that?

1 Answer 1


The collection [^s%] matches exactly one character that's not an 's' or a '%'. Put quotes around it and it will match one of the said character in quotes. And "mm" is two characters in quotes.
That's why.

This is documented in :help /[].

Remove the trailing quote and it will match "unit": "m

Or add a count like /"unit": "[^s%]\{1,2}" so it matches one or two characters. More generally, /"unit": "[^s%]*" to match no or any number of not s or % between quotes (as suggested by Barmar).

All these largely do the same and differ in details. It depends on what you want to achieve.

One more piece of advice: when constructing search patterns, :set hlsearch is very useful. It will highlight matches as you type your pattern. If the highlight goes away, you know your last keystroke caused it and can backtrack. See :help 'hlsearch'.

  • /me discards answer
    – romainl
    Apr 18 at 6:23
  • @romainl your answer would have been much more elaborate and thorough, I am sure. And Vivian keeps on outracing me. It's a sting. I know.
    – Friedrich
    Apr 18 at 6:24
  • 1
    Or [^s%]* to match any number (including 0) of "not s or %" between quotes.
    – Barmar
    Apr 18 at 14:33
  • @Barmar I thought it was obvious :-) OP may find it useful, I edited it in. Thank you.
    – Friedrich
    Apr 19 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.