Page 1 of 1

Regex to remove comment lines

Posted: 01 Jul 2019, 10:38
by john_c

Code: Select all

Test =
(
;       Foo
;Bar
;  aaa   aaaaa
Baz
)

Test := RegExReplace(Test, "???", "")
MsgBox % Test
Please, help me with regex. I need to remove any comment lines. I.e. only the Baz line should be kept.

Re: Regex to remove comment lines  Topic is solved

Posted: 01 Jul 2019, 10:48
by Datapoint
"`am)^\s*;.*(?:\R|$)"

Re: Regex to remove comment lines

Posted: 01 Jul 2019, 10:53
by john_c
@Datapoint Thanks for your input, but the MsgBox is blank...

upd: Ah, after your edit it works. Thanks again.

Re: Regex to remove comment lines

Posted: 01 Jul 2019, 10:54
by Datapoint
edited my previous post to add the `a
https://regex101.com/r/618UAe/1

Re: Regex to remove comment lines

Posted: 01 Jul 2019, 11:33
by teadrinker
If the comment line is the last one, the line break won't be removed by this way:

Code: Select all

Test =
(
;       Foo
;Bar
;  aaa   aaaaa
Baz
;  last comment
)

Test := RegExReplace(Test, "`am)^\s*;.*(?:\R|$)")
MsgBox % "|" Test "|"
Better like this:

Code: Select all

Test =
(
;       Foo
;Bar
;  aaa   aaaaa
Baz
;  last comment
)

Test := RegExReplace(Test, "m`a)(\R)?^\s*;.*(?(1)|\R)")
MsgBox % "|" Test "|"

Re: Regex to remove comment lines

Posted: 01 Jul 2019, 11:54
by john_c
@teadrinker Thanks! (Ah, I was sure I know something about regular expressions!)

Re: Regex to remove comment lines

Posted: 01 Jul 2019, 12:01
by teadrinker
@john_c, welcome here: http://www.pcre.org/pcre.txt :)