regex: how to find two recurring “ char within a line

76 Views Asked by At

I am having hundreds of lines as illustrated below, with more than one opening double-quote () occurring within almost every line as shown below:

... “ ...  “ ..... “ .....

note: those dots (...) above denote both words & spaces in this context for illustrative purposes.

  1. How to search (via regex) for every such occurrence within every line? I tried achieving this with:

    “.*“ or, “.* “

    but it is disappointingly returning even those who are proper i.e., with both opening & closing double quotes also (which is the correct way it should be) as follows:

    ... “ ...” ..... “ .....” ...... “ .....

  2. For every second [space]“ recurring within every line it encounters — How to replace them (via regex) into ” [space]?

2

There are 2 best solutions below

4
TEXHIK On BEST ANSWER

use [^”]* instead of .*, so it will search all occurence of two opening quotes with any character sequence in between except of closing qoute.

EDIT:

“[^”]*?“ -- miss, that it will find largest srting between two opening quotes (OQ) as possible, in “some text “more text “text it will find “some text “more text “, so you need ? after *.


And as of your pictures, you are using sublime, so replace (“[^”]*?)\s“ with \1”

  • () capturing a group, which you can access later with \n, where n is group number.
  • *? lazy expression, stop at first occurence of next character (\s here)
  • \s any whitespace character (space, tab, new line, etc.)
  • \1 first captured group, here - opening quote and some text

It is possible to use look behind (?<=text), but it length must be known, in your exampole its length is unknown (because of *).

3
Hermes Martinez On

If you search for s/(“.*?)(“)/, you could replace every second occurrence of into by r/(“.*?)(“)/$1”/g

.*? as a lazy operator would make it stop right on the second occurrence.