Regex - Remove block comments

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Regex - Remove block comments

Post by m3user » 09 Apr 2023, 15:41

Hi, I was searching the forums but cannot find the answer how to remove block comments (/* ... */) from the script (string). The closest is this regex found on viewtopic.php?t=687

Code: Select all

string:= RegExReplace(string, "ms`a)^\s*/\*.*?^\s*\*/\s*")	; Removes /* ... */
The issue is that it deletes other empty lines and it doesn't consider open comments (without ending with */).
Any idea of a better function? Thanks.

Test string:

Code: Select all

Line1

/* comment
comment
*/ 
Line2
  /* comment
  comment
  */ Line3

Line4
/* comment
Line5
Line6
Expected result:
Line1

Line2
Line3

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

Re: Regex - Remove block comments

Post by mikeyww » 09 Apr 2023, 16:59

Code: Select all

#Requires AutoHotkey v1.1.33
str   := "
(
Line1

/* comment
comment
*/ 
Line2
  /* comment
  comment
  */ Line3

Line4
/* comment
Line5
Line6
)"
regex := "\h*?/\*.*?(\*/\s*|$)"
str2  := Trim(RegExReplace(str, regex), "`n")
MsgBox 64, Result, % str2
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 10 Apr 2023, 05:04

Thanks Mikeyww, much appreciated. I didn't know about \h, not in the docs. Just to understand better, if the string would have a another character in places for new lines, for example ¶ - how would the regex look like?

Code: Select all

string:="/*¶comment¶*/¶Line1¶¶/* comment¶comment¶*/¶Line2¶  /* comment¶  comment¶  */ Line3¶¶Line4¶/* comment¶Line5¶Line6"
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Regex - Remove block comments

Post by Descolada » 10 Apr 2023, 05:38

I believe you could use something like this as well:
str2 := RegexReplace(str, "\/\*[\s\S]*?(\*\/|$)")
This one doesn't care about what the newline character is.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 10 Apr 2023, 09:41

Thanks. Unfortunately this adds several additional new lines. Can this be improved?
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Regex - Remove block comments

Post by Descolada » 10 Apr 2023, 13:50

It doesn't add any new lines, rather it doesn't remove ones that already exist - it removes only the comment block, leaving everything else untouched. This means that ‎ /* comment */ will get turned to ‎ ‎ (four spaces), not an empty string. Similarly the surrounding newlines are not removed. This one also removes preceding whitespaces and following whitespaces/newlines: RegExReplace(str, "\h*\/\*[\s\S]*?(\*\/\s*|$)")
But this means that

Code: Select all

Line1

/* comment
comment
*/


Line2
would be turned into

Code: Select all

Line1

Line2
not

Code: Select all

Line1


Line2
Whether this is desirable is up to you...

@mikeyww, learned something new today from you: AHK's regex symbol . matches \n and \r but not \n\r, so my [\s\S] was actually not necessary. And also that forward-slashes don't need to be escaped... This meant that to make your Regex work in regex101.com I had to modify it to \h*?\/\*[\s\S]*?(\*\/\s*|$).
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 10 Apr 2023, 14:32

Thanks, what I meant is that the result of the two regex commands should be the same.
Here's an example. The first msgbox shows the result of regex by mikeyww and is correct. The result of second regex (after replacing new lines with ¶) is not as expected - extra lines are added.

Code: Select all

str:="
(
Line1

/* comment
comment
*/ 
Line2
  /* comment
  comment
  */ Line3

Line4
/* comment
Line5
Line6
)"

str2  := Trim(RegExReplace(str, "\h*?/\*.*?(\*/\s*|$)"), "`n")
MsgBox 64, Result, % "|" str2 "|"

Stringreplace, str, str, `n, ¶, all
str2 := RegexReplace(str, "\h*?\/\*[\s\S]*?(\*\/\s*|$)")
Stringreplace, str2, str2, ¶, `n, all
MsgBox 64, Result, % "|" str2 "|"

return
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 11 Apr 2023, 09:01

Sorry, this is not working.
Chunjee wrote:
10 Apr 2023, 17:09
I used Om)^(\h*;.*)(?:\R\g<1>){3,}
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Regex - Remove block comments

Post by Descolada » 11 Apr 2023, 10:31

You can add any custom newline to the last \s part of the regex, for example RegexReplace(str, "\h*?\/\*[\s\S]*?(\*\/[\s¶]*|$)"). You can combine it with Trim to remove any excess `n from the beginning and end.
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 11 Apr 2023, 11:21

Thank you very much! This should be it. :)
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: Regex - Remove block comments

Post by m3user » 14 Apr 2023, 11:35

This works str2 := RegexReplace(str, "\h*?/\*.*?(\*/[\s¶]*|$)")

However it removes /* .... */ comments (in one line). According to Ahk documentation this is not a block comment so it should not be removed.
In addition, the /* and */ symbols can be used to comment out an entire section, but only if the symbols appear at the beginning of a line (excluding whitespace), as in this example:
https://www.autohotkey.com/docs/v1/Language.htm#comments

Any idea how to improve the Regex?
Post Reply

Return to “Ask for Help (v1)”