5

I followed the question in another post: Regex to capture LaTeX comments

The provided answer is awesome. However, it seems like it can only be used in the .net engine, but I want to use the PCRE engine. That is because I'm using sublime text and it seems like this engine is used by default. I tried many times, but without success.

The latex is

\usepackage{test}%COMMENT1

TEXT
%COMMENT2
TEXT

Value is 10\%, this should not be removed. %COMMENT3

begin{tikz}[
important 1,
%COMMENT4
important 2, %COMMENT5
]

TEXT
%COMMENT 6

TEXT

Table: value1&value2\\%COMMENT7
Table: value1&value2\\      %COMMENT8
Table: value1&value2            \\%COMMENT 9
Table: value1&value2\\%            COMMENT 10
Table: value1&value2\\%COMMENT11       

I tried (?m)(?<=(?<!\\)(?:\\{0}))%.*(?:\r?\n(?!\r?$))?. Only works on comment 1-6,8.

The online results can be found https://regex101.com/r/zSIBMu/3

How should I modify it?

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
stackname
  • 101
  • 2
  • 9
  • Do you also want to remove `\\\%COMMENT` ? – The fourth bird Dec 17 '21 at 23:26
  • 1
    @Thefourthbird Probably not. Because "\\" will start a new line and '\%' will print out '%' itself. So, this is not a comment in latex. – stackname Dec 17 '21 at 23:39
  • 1
    Does this answer your question? [Sublime text 2 how to delete comments only](https://stackoverflow.com/a/44572917/4473405) – Keith Hall Dec 18 '21 at 06:11
  • @KeithHall Thanks a lot. I never thought of it in this way. This is pretty useful and can be used for all types of comments without using any new regex. Thanks. – stackname Dec 18 '21 at 21:59

2 Answers2

4

With a capture group:

((?<!\\)(?:\\\\)*)%.*

and with $1 as replacement.

demo

Or perhaps, if \K is supported:

(?<!\\)(?:\\\\)*\K%.*

with an empty string as replacement.

demo

Notice: I don't see any reason to remove the newline sequence even if the comment takes all the line.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Thanks. This is working. But I'm not sure if I understand the meaning of your notice. – stackname Dec 17 '21 at 23:24
  • @whatsname: That means that if a comment is at the start of the line, the replacement doesn't delete the line (only the comment). – Casimir et Hippolyte Dec 17 '21 at 23:49
  • Thanks for the explanation. I think this is needed when there are multiple consecutive single lines of comments. Then, deleting them would make it look better than replacing them with white space. – stackname Dec 18 '21 at 01:32
4

You might also make use of a SKIP FAIL approach:

\\(?<!\\.)(?>\\\\)*%(*SKIP)(*F)|%.*

The pattern matches:

  • \\(?<!\\.) Match \ not preceded with \
  • (?>\\\\)* Match optional pairs of \\
  • %(*SKIP)(*FAIL) Match % and skip the match
  • | Or
  • %.* Match % and the rest of the line

Regex demo

Edit

A non lookaround solution suggested by Casimir et Hippolyte to skip the match for \\ or \%

\\[\\%](*SKIP)(*F)|%.*
  • \\[\\%] Match either \\ or \% using a character class
  • (*SKIP)(*FAIL) Skip the match
  • | Or
  • %.* Match % and the rest of the line

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70