5

How can I create a regex for Vim that accommodates for matching multiple double quotes strings on one line, without matching the text in between the two double quotes strings? A restriction on the pattern is that the double quoted strings can't contain a single quote. So far I came up with /"\([^']\{-}\)"/ to match the strings below. But as you see it will match the text in between the strings for the second line. I can't rely on white space surrounding the strings, as you see in the third line. And of course it needs to work with the fourth line as well.

  • "cat" is called "foo"
  • "cat's" name is "foo"
  • x="cat's food"
  • x = "cat"
7
  • it would be a tricky task for regex, for example "cat's" name is "foo" the 2nd " (after 's) should or should not be matched? the answer is negative, I knew. because of cat's. but " name is " is fine... I think you got what I meant. Commented May 9, 2014 at 9:30
  • I don't quite understand what exactly it is you want to match. Could you add an explanation to your four examples of what parts of the line you want to match? Commented May 9, 2014 at 9:31
  • Thanks. I would like to match all the double quoted strings in the example, excluding the ones which contain a single quote. So I want "cat", "foo", "foo", "cat" and not "cat's" or "cat's food". Commented May 9, 2014 at 9:42
  • @kodnin wait, you want to only match the double quotes (") or the double quotes and the string wrapped by them, if the string doesn't contain single quote '? you better edit your question, to explain a bit. e.g. line: xxx, what you want to get is yyyy use the code block pls. Commented May 9, 2014 at 9:54
  • @kent, basically I want to retrieve the contents from within the double quotes. This way I can replace them with single quotes. It goes without saying that I do not want to replace double quotes for single quotes when there is a single quote inside. Commented May 9, 2014 at 9:57

2 Answers 2

2

basically I want to retrieve the contents from within the double quotes. This way I can replace them with single quotes. It goes without saying that I do not want to replace double quotes for single quotes when there is a single quote inside

I didn't find a way to write a simple regex to match your need, but with vim's :s there is a way:

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

after you executed the above line, your example text would be changed into:

'cat' is called 'foo'
"cat's" name is 'foo'
x="cat's food"
x = 'cat'
Sign up to request clarification or add additional context in comments.

1 Comment

That looks interesting, appreciate the effort! Still wondering if this line could be refactored into something more simple.
1

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

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

matched all the strings from your example that are in double quotes, but not those containing single quotes.

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

[highlighted here means: found by the regex search in vim when prompted the command from above]

This uses the \@<! construct which is vim-regex syntax for negative look-behind (see vim manual here). Matches double quotes that are not preceded by a quote and a single quote. This still has trouble if a single quote exists outside of double quotes though. I don't know if thats a problem, let me know if it is.

1 Comment

I don't want the strings containing the single quote, like "cat's" and "cat's food".

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.