Regex to remove all commentaries in the script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Regex to remove all commentaries in the script

Post by Chappier » 27 Oct 2021, 17:48

I searched into the source of Ahk2Exe to see how does it clear all commentaries in the script before compiling it, and found the regex above:

Code: Select all

"ms`a)(^\s*/\*.*?^\s*\*/\s*\v|(?-s)(^\s*;.*\R|[ \t]+;.*)|^\s+?\v)"
The regex also clears empty lines, and clears the commentaries lines.
Im trying to clear the commentaries but keep their lines in the same place, example:

Code: Select all

/*
Commentaries
*/
;Commentaries
x := 1 ;Commentaries
to

Code: Select all





x := 1

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Regex to remove all commentaries in the script  Topic is solved

Post by mikeyww » 27 Oct 2021, 21:43

Code: Select all

str =
(
/*
Commentaries
*/
;Commentaries
x := 1 ;Commentaries
)
MsgBox, 64, Result, % noComment(str)

noComment(str) {
 str := RegExReplace(str, "m`a)(\h+|^);.*")
 While RegExMatch(str, "(.*?)(\n|\A)/\*(.*?)(\n)\*/(.*)", m)
  str := m1 m2 RegExReplace(m3, "[^\v]") m4 m5
 Return str
}

garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Regex to remove all commentaries in the script

Post by garry » 28 Oct 2021, 03:31

@mikeyww , thanx, you're anytime here with good solutions and answers ...

Post Reply

Return to “Ask for Help (v1)”