cmeeren
cmeeren

Reputation: 4210

regex to match multiple times per line and across lines, but not after comment

I have the following multi-line string:

asd \cite[1][2]{foo, bar} asdf \cite{baz, bar}
foo \citet{lorem} % \cite{asd}
foo \citep{ipsum}
\citep{dolor, sit,
amet}

I want to match everything inside the curly braces (including dolor, sit, amet), but not if it's commented out (after %). I don't know what comes after the comment - it may be any number of spaces or characters. I currently use the following regex:

 \\cite[tp]?\s*\[?.*?\]?\s*\{(.*?)\}.*?

This also matches the commented out part. How can make sure that doesn't happen?

See example on regex101

Upvotes: 1

Views: 434

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You can use a negative look-behind for that :

(?<!%)\s\\cite[tp]?\s*\[?.*?\]?\s*\{(.*?)\}.*?

Demo

Upvotes: 1

Related Questions