2

I'm trying to fix a csv file but the regex expression won't work.

I want to jump to lines that have a number of commas that is different than 5. I first select lines with less than 5 so I press / and type

^[^,\r\n]*(?:,[^,\r\n]*){0,4}$`

but it won't match anything. It's not the first time that I can't match a regex that is otherwise working on other engines, what is happening ?

0

1 Answer 1

3

Vim regex engine doesn't work exactly like other engines. It includes different 'magic modes' which change the characters that you have to escape.

See :h /magic for a list of the characters to escape and more generally :h pattern.txt to learn how to use regexes on Vim.

To make your search work you should use:

^[^,\r\n]*\(,[^,\r\n]*\)\{0,4}$
2
  • What about lines that have more or less than 19 semi colons ? I tried this: /^[^;\r\n]*\(;[^;\r\n]*\)\{0,19}$
    – Stephane
    Commented Jul 24, 2017 at 13:16
  • 1
    @Stephane you're matching between 0 and 19 semi colons with your regex. To do what you want I think you'll have to create a more complex regex which will match lines with less than 19 semi colons and lines with more than 19 semi colons. Depending on what you actually want to do, you could also use a regex which matches exactly 19 semicolons ({19}) and the :v command which applies a command on the lines which are not matched by the regex. See :h :v and maybe ask a new question if you need more help :-)
    – statox
    Commented Jul 24, 2017 at 18:22

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.