Regex to help me replace VSCode Snippets

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Regex to help me replace VSCode Snippets

Post by jsong55 » 19 Sep 2021, 04:01

Code: Select all

"Snippet for AHK Forum": {
  "prefix": "Snippet for AHK Forum",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},
Problem, how do I create a regular expression to match all the information inside of { to },

in the first example above, the match should give

Code: Select all

  "prefix": "Snippet for AHK Forum",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
teadrinker
Posts: 4295
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex to help me replace VSCode Snippets

Post by teadrinker » 19 Sep 2021, 06:08

Code: Select all

text =
( %
"Snippet for AHK Forum": {
  "prefix": "Snippet for AHK Forum",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},
)
MsgBox, % RegExReplace(text, "s).+?(?<=\{)(.+?)(?=\R}).*", "$1")
jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Regex to help me replace VSCode Snippets

Post by jsong55 » 19 Sep 2021, 06:17

Wow thanks!

Learnt something new here with the greed usage

Code: Select all

s)".+?(?<=\{)(.*)(?=\},).*"
Some interesting new things I realized,

\R matches the new line

and .+? prevents greed

The lookback also is particularly helpful. Previously I only used look ahead and couldn't solve the problem
Last edited by jsong55 on 19 Sep 2021, 06:29, edited 1 time in total.
User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Regex to help me replace VSCode Snippets

Post by boiler » 19 Sep 2021, 06:28

jsong55 wrote:

Code: Select all

s)".+?(?<=\{)(.*)(?=\},).*"
Also works.
Not really. Try it with this:

Code: Select all

"Snippet for AHK Forum": {
  "prefix": "Snippet for AHK Forum",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Yet another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},
jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Regex to help me replace VSCode Snippets

Post by jsong55 » 19 Sep 2021, 06:33

Ah I managed to get the answer to my own question below,

Code: Select all

MsgBox, % RegExReplace(text, "s)""Another Snippet Example"": {(?<=\{)(.+?)(?=\R\}\,)(.+?)", """Snippet Name"": {`nI'm cheeky`n$2")
Yes you are right, the .+? and .* makes a ton of difference

Now I'm trying to replace the middle snippet but can't seem to get rid of the extra "},"

What should I do?

Code: Select all


text =
( %

"Snippet for AHK Forum": {
  "prefix": "Snippet for AHK Forum",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},

"Yet another Snippet Example": {
  "prefix": "Another Snippet Example",
  "body": [
    "; This snippet was created for VSCODE",
"${1:VarName}:=\"Hello World\"",
"Msgbox,% A_LineNumber",
"Msgbox,% \"${2:Custom Message}\"",
"ToolTip,% ${1:VarName}r"
  ],
  "description": "to demonstrate snippet changes"
},
)
MsgBox, % RegExReplace(text, "s).+?(?<=\{)(.+?)(?=\R}).*", "$1")
MsgBox, % RegExReplace(text, "s)""Another Snippet Example"": {(?<=\{)(.+?)(?=\R\},)(.*)", """Snippet Name"": {`nI'm cheeky`n},$2")

teadrinker
Posts: 4295
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex to help me replace VSCode Snippets

Post by teadrinker » 19 Sep 2021, 07:05

Code: Select all

MsgBox, % RegExReplace(text, "s)""Another Snippet Example"":.+?\R},", """Snippet Name"": {`nI'm cheeky`n},")
jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Regex to help me replace VSCode Snippets

Post by jsong55 » 19 Sep 2021, 18:48

teadrinker wrote:
19 Sep 2021, 07:05

Code: Select all

MsgBox, % RegExReplace(text, "s)""Another Snippet Example"":.+?\R},", """Snippet Name"": {`nI'm cheeky`n},")
AWESEOM :bravo: :bravo:
Post Reply

Return to “Ask for Help (v1)”