regex - Replace double quoted strings by single quoted except for GStrings -


my ocd has gotten better of me , i'm going through groovy codebase replacing simple strings double quotes around them single quoted strings.

however, want avoid gstrings contain dollar symbols , variables.

i'm using intellij substitution, , following works:

from: "([^$\"\n\r]+)"   to: '$1' 

it captures strings without dollars in, partially skips strings contain them.

for example matches quotes between 2 double quoted strings in case:

foo("${var}": "bar")            ^^^^ 

is possible create regex skip whole string contained dollars, in above case skips "${var}" , selects "bar", instead of erroneously selecting ": "?

edit: here's section of code try against

table.columns.elements.each{ columnname, column ->   def columntext = "${columnname} : ${column.datatype}"   cols += "${columntext}\n"   if (columntext.length() > width) {     width = columntext.length()   }   height++ } builder."node"("id": table.elementname) {   builder."data"("key": "d0") {     builder."y:shapenode"()   } }  def foo() {   def string = """ multiline quote using triple quotes ${var} gstring vars in. """ } 

do single , triple quote replacements separately.

single quotes:

use ahead number of quotes after hit. negative behind stops matching inner quotes of triple quoted strings.

find: (?<!")"([^"$]*)"(?=(?:(?:[^"\r\n]*"){2})*[^"]*$) replace: '$1' 

see live demo.

triple quotes:

use simpler match triple quoted strings, since on own lines.

find: """([^"$]*?)""" replace: '''$1''' 

see live demo, includes triple-quoted string contains variable.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -