Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode regex replace using regex group

I want to replace map["someKey"] as double by (map["someKey"] as num).toDouble(), but I just can't get the regex working correctly. Can someone who knows more about regex tell me what to put in Find and Replace in VSCode? I've tried ([a-zA-Z0-9]*) as double and replaced this by ($1 as num).toDouble(), but this doesn't work.

like image 230
Florian Avatar asked Sep 03 '26 09:09

Florian


1 Answers

Use

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

See regex proof. Replace with ($1 as num).toDouble().

EXPLANATION

--------------------------------------------------------------------------------
  (                        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'
like image 149
Ryszard Czech Avatar answered Sep 06 '26 00:09

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!