Vim regex for string matching

How to create a regex for Vim that matches strings with two double quotes on the same line, without matching text between two strings of double quotes? The restriction on the pattern is that double-quoted strings cannot contain a single quotation mark. So far I have come up with /"\([^']\{-}\)"/to fit the lines below. But, as you can see, it will match the text between the lines for the second line. I cannot rely on the white space surrounding the lines, as you see in the third line. And, of course, it should work with the fourth line.

  • "cat" is called "foo"
  • "cat" name "foo"
  • x = "cat food"
  • x = "cat"
+4
source share
2 answers

Basically I want to get the contents from a double quote. That way I can replace them with single quotes. This goes without saying that I don't want to replace double quotes for a single quote when inside

I have not found a way to write a simple regex according to your needs, but with vim :sthere is a way:

%s/\v"([^"]*)"/\=stridx(submatch(1),"'")>=0?submatch(0):"'".submatch(1)."'"/g

after executing the above line, your example text will be changed to:

'cat' is called 'foo'
"cat's" name is 'foo'
x="cat food"
x = 'cat'
+2
source

I'm not quite sure that I understand what you need here, but

/\("\([^"]*'[^"]*\)\)\@<!\("\([^"^']*\)"\)

matches all lines from your example that are in double quotes, but not those that contain single quotes.

"cat" is called "foo"   => "cat", "foo" highlighted
"cat's" name is "foo"   => "foo" highlighted
x="cat food"          => nothing highlighted
x = "cat"               => "cat" highlighted

[ : vim ]

\@<!, vim-regex (. vim ). , . , . , , , .

+1

Source: https://habr.com/ru/post/1539894/


All Articles