Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

json - 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.

question from:https://stackoverflow.com/questions/65946686/regex-to-replace-spaces-in-quoted-strings-with-sublime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 (
, 
, , 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
--------------------------------------------------------------------------------
  ":                       '":'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...