How can I match all occurrences of a phrase not tagged with the required LaTeX tag?

purpose

Find all occurrences of "Bernie Sanders" that are not marked \senator{ }. I want to list matches with grepfor visual inspection. Then I would like to fix all files recursively with a single command (for example sed, which does not support non-greedy regular expression).

Sample file

Bernie Sanders
\senator{Bernie Sanders}
The senator of Vermont is \senator{Bernie Sanders}.
A \texttt{senator of Vermont} is Bernie Sanders.
A senator of Vermont is \textit{Bernie Sanders}.
\textit{Bernie Sanders} is a senator of Vermont.
Is this the same Bernie Sanders?
Is Bernie Sanders a good senator?
Will we ever see a \textbf{President Bernie Sanders}?

Problem

A regular expression should not "accidentally" interfere with other commands

Attempt: \[^senator]*{Bernie Sanders

I'm not sure how to exclude the senator, but include spaces and other teams in front of Bernie Sanders.

Start

  • can be done with a space
  • not running \ senator {
  • may be first in line

End

  • , , , , , , ( ), /
+4
4
(?<!\\senator{)Bernie Sanders(?!\s*})

grep -P. Lookarounds, , senator. . .

https://regex101.com/r/vV1wW6/7

+1

\

(?<!\\senator\{)Bernie\sSanders
+1

perl script. ( ):

perl -pe 's/(?<!\\senator{)Bernie\sSanders/The New Bernie Sanders/g' input.tex > output.tex

regexp input.tex "The New Bernie Sanders". output.tex.

, script bash script :

#!/bin/bash    
for i in {1..3}
do
    perl -pe 's/(?<!\\senator{)Bernie\sSanders/The New Bernie Sanders/g' input$i.tex > output$i.tex
done

script input1.tex, input2.tex, input3.tex output1.tex, output2.tex, output2.tex.

( , , , bash script).

+1

grep ( :\senator {Bernie Sanders} , )

grep "Bernie Sanders" input.tex |  grep -v -e '\\senator{Bernie Sanders}'
0

Source: https://habr.com/ru/post/1606437/


All Articles