使用正则表达式组进行 VSCode 正则表达式替换

3

我想用(map["someKey"] as num).toDouble() 替换 map["someKey"] as double,但我无法正确使用正则表达式。 有了解更多关于正则表达式的人可以告诉我在VSCode中找到什么并替换为什么吗? 我尝试过 ([a-zA-Z0-9]*) as double 并将其替换为 ($1 as num).toDouble(),但这行不通。


map["someKey"] 总是一个字典对象吗? - Wiktor Stribiżew
是的,对不起,我先写了 var 然后想用 map["someKey"] 更精确,但是我忘记把它放在第一位了。 - PlutoHDDev
1个回答

4

使用

(\w+\["[^"]+"\]) as double

请查看正则表达式验证。使用($1 as num).toDouble()进行替换。

说明

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \[                       '['
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    [^"]+                    any character except: '"' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    "\]                       '"]'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
   as double               ' as double'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接