I tried to use RegReplace to create a simple sequence to swap quotes of all double quoted strings, or all single quoted strings. I wanted to ensure it woud work in all languages regardless of whether they used “double quoted” or “single quoted” scope types. It was really hard, so I figured there had to be a better way. This lead me to add a new feature.
Version 0.9
- Allow multipass on a scope region with regex with new “multi_pass_regex” parameter
This allowed me to use a scope search with regex qualifiers, and identify all string regions. I then used regex to determine if they were single or double quoted and find all internal quotes that needed to be escaped and unescaped within the string.
Regex defines for swapping from double to single quotes
"swap_quotes_to_single": {
"scope": "string",
"find" : "^\"(.*?)\"$",
"replace": "'\\1'",
"greedy_replace": false
},
"escape_single_quotes": {
"scope": "string",
"find" : "^(\".*?(?<!\\\\))((?:\\\\]{2})*)'(.*?\")$",
"replace": "\\1\\2\\'\\3",
"greedy_replace": false,
"multi_pass_regex": true
},
"unescape_quotes_double": {
"scope": "string",
"find" : "^('.*?(?<!\\\\))((?:\\\\]{2})*)\\\\\"(.*?')$",
"replace": "\\1\\2\"\\3",
"greedy_replace": false,
"multi_pass_regex": true
},
Regex defines for swapping for single to double quotes
"swap_quotes_to_double": {
"scope": "string",
"find" : "^'(.*?)'$",
"replace": "\"\\1\"",
"greedy_replace": false
},
"escape_double_quotes": {
"scope": "string",
"find" : "^('.*?(?<!\\\\))((?:\\\\]{2})*)\"(.*?')$",
"replace": "\\1\\2\\\"\\3",
"greedy_replace": false,
"multi_pass_regex": true
},
"unescape_single_quotes": {
"scope": "string",
"find" : "^(\".*?(?<!\\\\))((?:\\\\]{2})*)\\\\'(.*?\")$",
"replace": "\\1\\2'\\3",
"greedy_replace": false,
"multi_pass_regex": true
}
The commands
// Swap Quotes
{
"caption": "Replace: Swap Quotes to Single",
"command": "reg_replace",
"args": {"replacements": "escape_single_quotes", "swap_quotes_to_single", "unescape_double_quotes"], "find_only": true}
},
{
"caption": "Replace: Swap Quotes to Double",
"command": "reg_replace",
"args": {"replacements": "escape_double_quotes", "swap_quotes_to_double", "unescape_single_quotes"], "find_only": true}
}
Now it is easy .