Home > Blockchain >  How to use VSCode regexp to find all ocurrences of \cite without ~?
How to use VSCode regexp to find all ocurrences of \cite without ~?

Time:11-30

I need to find in several files of my thesis all occurrences of any text\cite{...} and change them to any text~\cite{...}. But I don't understand how VSCode search regexp works.

I have tried ^[!~]\cite but it doesn't seem to understand this notation for a char that is not ~.

Can anyone help?

CodePudding user response:

An alternative to Wiktors answer could be:

(?<!~)(\\cite)

And replace with:

~$1

(?<!~) is a negative lookbehind forcing the regex to only match something not starting with a tilde (~). (\\cite) matches the text literally, where the parentheses creates a capture group.

In the replacement first you add the tilde you want added, followed by $1 which is a back reference, meaning "take capture group 1 and put here".

Groups are numbered from 0 (which is the entire match). And is numbered from outside in, left to right. The negative lookbehind is not a capture group.

CodePudding user response:

You can use

([^~]|^)(\\cite)

Replace with $1~$2, see the regex demo.

Details:

  • ([^~]|^) - Group 1: any char other than ~ ([^~]) or (|) a start of a line
  • (\\cite) - Group 2: \cite text.

The $1~$2 replacement pattern uses Group 1 ~ Group 2 value to replace the matches.

  • Related