Vinay Pandey
Vinay Pandey

Reputation: 1199

Search and replace colon (:) between double quotes using regular expressions

I am fairly new to regular expression therefore this may be the simplest question you've seen on StackOverflow :-)

I have a large JSON file with text like this:

{..., "text": "BLAH BLAH", ...}

The text may contain any of the special characters and also characters like \", which I understand can be seen as escape character in regular expression. I am trying to find and replace a single character colon : with tilde ~ within the portion followed by "text" preferably in Notepad++. Any help will be greatly appreciated.

Upvotes: 1

Views: 2837

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89649

You can do this:

find: ("(?:[^"]+|(?<=\\)")*")\s*:
replace: $1~

The idea is to capture the content inside double quotes, to put it in the replacement.

I use a lookbehind to allow escaped double quotes inside double quotes.

Upvotes: 0

Ro Yo Mi
Ro Yo Mi

Reputation: 15010

This regex will find all the : in the value for fields named text and replace the character with a ~. Note there were issues using regular expressions with Notepad++ v5. My demo here was tested in Notepad++ v6.3.3

Regex: ("text":\s"[^"]*?):

Replace with: $1~

Input string: {"not text": "12:34", "text": "BLAH:BLAH", "Never get a": ":oskupee"}

enter image description here

Upvotes: 1

Vinay Pandey
Vinay Pandey

Reputation: 1199

Here is what I did (thanks for all the help @Mike, but I had to make many edits. That's why I am answering my own question so other users can get the complete answer)

  1. Search for \"text\": \".*? : .*?\", in Notepad++
  2. Find and replace with \1~\2 to replace all : with ~
  3. Manually correct the mistakes

Upvotes: 0

Related Questions