0

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 Answers2

0

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 *).

TEXHIK
  • 1,369
  • 1
  • 11
  • 34
  • thank you so much! your solution is working wonderfully and it returned me 2190 such lines matching this criteria. Now, is there a way to make the replacement as I asked in the second question? For e.g., [this picture](https://i.snag.gy/LEUvx6.jpg) is one example of the results it returned; Of all these matches, what RegEx command should I type to replace **every alternate space followed by an opening double quote char** i. e., `[space] “` into `” [space]` as [this](https://i.snag.gy/J6An89.jpg) which is **a closing double-quote followed by space**? – VinayaŚiṣya May 11 '16 at 14:14
  • added answer to second part. – TEXHIK May 12 '16 at 07:13
  • Quoting you ❝ so replace `(“[^”]*?)\s“` with `\1” ` ❞ — End quote. It is replacing the whole matched-string [e.g., as shown here](https://i.snag.gy/xk1sdj.jpg) to completely blank as [this](https://i.snag.gy/g0Kfod.jpg) instead of replacing it with (1) Closing-quote & (2)space, as explained in my question. Where did I go wrong? – VinayaŚiṣya May 12 '16 at 22:20
  • It is strange, because `\1` is standard group defenition. And it works well in my sublime. But try to use `$1`, it is working too. – TEXHIK May 13 '16 at 06:58
0

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.

  • thanks for your suggestion. However, it doesn't work. Conversely, TEXHNIK's solution that he has given below works. – VinayaŚiṣya May 11 '16 at 13:49
  • **UPDATE**: Since I am working on Sublime Text, therefore your codes, as you have provided, did not work (maybe it works for vim). However, after altering them to simply `(“.*?)(“)`, I just realised it's working! Yuppy. Furthermore, altering your replace solution to simply `$1”` works but it leaves a space which preceeds the closing-quote like this: `[space]”`. What I need in the replacement code is it should automatically shift the space after the closing quote, to this: `”[space]`. Is there a way to make that via RegEx? – VinayaŚiṣya May 12 '16 at 22:49
  • Yeah just type it on your replace field – Hermes Martinez May 13 '16 at 05:00