2017-09-13 3 views
-1

리눅스 명령 줄에서 Fig.으로 그림 참조를 참조하지 않는 여러 파일에서 모든 인스턴스를 찾고 싶습니다.Regex/grep match on : "not this"and "that"

\ref{figFig.이 붙어 있지 않을 때 각 줄을 찾고 있습니다. \ref{fig:myFigure}

  • A sentence with \ref{fig:myFigure} there.
  • 정규식

  • A sentence with Fig. \ref{fig:myFigure} there.
  • Fig. \ref{fig:myFigure}
    1. 사례를 무시한다 (1) 및 (2) 만, 예를 찾기 (3), (4).

    +1

    무엇'Fig.'와'\의 ref' 사이 바꿈에 대한? –

    +0

    @ BenjaminW. - 네, 그럴 가능성이 있습니다. 나는 원래 게시물에서 "not"와 "that"에 필요한 regex에 집중하기 위해 생략했다. 선택적 줄 바꿈은 추가하기가 어렵지 않습니다. –

    +0

    선택적 줄 바꿈은 큰 차이를 만듭니다. grep은 줄 단위로 파일을 찾습니다. null 문자와 같이 개행 문자 대신에 다른 문자를 사용한다고 말하면, 일치는 한 줄로 간주되므로 전체 파일을 반환합니다. –

    답변

    1

    당신은 같은 부정적 예측을 사용할 수 있습니다

    ^((?!Fig\. {0,1}\\ref\{fig).)*$ 
    

    https://regex101.com/r/wSw9iI/2

    Negative Lookahead (?!Fig\.\s*\\ref\{fig) 
    Assert that the Regex below does not match 
    Fig matches the characters Fig literally (case sensitive) 
    \. matches the character . literally (case sensitive) 
    \s* matches any whitespace character (equal to [\r\n\t\f\v ]) 
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
    \\ matches the character \ literally (case sensitive) 
    ref matches the characters ref literally (case sensitive) 
    \{ matches the character { literally (case sensitive) 
    fig matches the characters fig literally (case sensitive)