【问题标题】:Regex to replace spaces in quoted strings with _ (Sublime)正则表达式用 _ 替换引用字符串中的空格(崇高)
【发布时间】:2021-01-28 23:27:26
【问题描述】:

我正在尝试通过删除空格(如果有的话)来修改我的 JSON 文件(大约 60 MB)中的键。我使用 Sublime Text Editor 来加载和编辑大型 JSON。

目前,我正在使用以下表达式来查找带空格的引号字符串:

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

发现:“名字”:

然后我使用下面的表达式,用匹配的字符串替换带下划线的空格:

"$1_$3":

结果:“t_Name”:

预期:“First_Name”:

我无法弄清楚为什么我无法用 $1 捕获第一个单词。任何帮助,将不胜感激。谢谢!

注意:JSON 中有大约 15000 个带有空格的不同键。

【问题讨论】:

  • ([a-zA-Z])+([\s])+ 是重复捕获组。它们仅在每个模式完成匹配后存储最后一个字符。只需将量词移动到组中,([a-zA-Z]+)\s+
  • 是的!感谢@WiktorStribiżew 的帮助

标签: json regex sublimetext3 sublimetext


【解决方案1】:

使用

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

proof

说明

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多