Search code examples
jsonregexsublimetext3sublimetext

Regex to replace spaces in quoted strings with _ (Sublime)


I am trying to modify keys in my JSON file (around 60 MB) by removing spaces if there are any. I use Sublime Text Editor to load and edit large JSONs.

Currently, I am using the following expression to find quoted strings with spaces:

"([a-zA-Z])+([\s])+([a-zA-Z]*)":

Finds: "First Name":

Then I use the following expression to replace space with underscore with the matched string:

"$1_$3":

Result: "t_Name":

Expected: "First_Name":

I am not able to figure out why I am not able to capture the first word with $1. Any help would be appreciated. Thanks!

Note: There are around 15000 different keys with spaces in the JSON.


Solution

  • Use

    "([a-zA-Z]+)\s+([a-zA-Z]*)":
    

    See proof

    Explanation

    --------------------------------------------------------------------------------
      "                        '"'
    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        [a-zA-Z]+                any character of: 'a' to 'z', 'A' to 'Z'
                                 (1 or more times (matching the most
                                 amount possible))
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \2:
    --------------------------------------------------------------------------------
        [a-zA-Z]*                any character of: 'a' to 'z', 'A' to 'Z'
                                 (0 or more times (matching the most
                                 amount possible))
    --------------------------------------------------------------------------------
      )                        end of \2
    --------------------------------------------------------------------------------
      ":                       '":'