Replace quotes inside quoted string with escaped q

2019-08-10 02:23发布

问题:

I am using Notepad++ to find (".*)"(.*) and replace it with \1\"\2 but it doesn't seem to work. I don't know why.

Example:
Someone said "My name is "sean""
I want it to be:
Someone said "My name is \"sean\""

Edit: In my case the closing quote is always on the end of line so will (".*)"(.*"$) work?

Edit2: Also the first quote is preceded with a comma so I will use (,".*)"(.*"$) though it may not work in some cases but I think it will work with my file.

Now there is the problem with the replace it doesn't add \" it just add some space.

回答1:

It should work... you just need to do a little fixing...

The Find what regex should be ("[^"]*)("\w*)(")([^"]*")
The Replace with expression should be \1\\\2\\\3\4

Make sure you select the Search Mode to be "Regular expression"

Explanation...

This is quite tricky - I've assumed that the quoted text WITHIN quotes is just a single word. If you assume something else it becomes very hard to pin down.

You need to find a
" followed by
[^"]* - any number of characters that are NOT a " and then
("\w*)(") - a quoted word, and then finally ([^"]*") - any additional number of non-quote characters + a final quote

This is important because regular expression matching is greedy by default, and a .* would continue to match all characters, including " until the end of the string (see link )

In the replacement string you need to have \\ to represent a single \