Search code examples
regexdartvisual-studio-coderegex-group

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.


Solution

  • 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'