Regex lookahead for 'not followed by' in grep

前端 未结 5 2000
忘了有多久
忘了有多久 2020-11-27 13:31

I am attempting to grep for all instances of Ui\\. not followed by Line or even just the letter L

What is the proper way to wr

5条回答
  •  离开以前
    2020-11-27 14:12

    If you need to use a regex implementation that doesn't support negative lookaheads and you don't mind matching extra character(s)*, then you can use negated character classes [^L], alternation |, and the end of string anchor $.

    In your case grep 'Ui\.\([^L]\|$\)' * does the job.

    • Ui\. matches the string you're interested in

    • \([^L]\|$\) matches any single character other than L or it matches the end of the line: [^L] or $.

    If you want to exclude more than just one character, then you just need to throw more alternation and negation at it. To find a not followed by bc:

    grep 'a\(\([^b]\|$\)\|\(b\([^c]\|$\)\)\)' *

    Which is either (a followed by not b or followed by the end of the line: a then [^b] or $) or (a followed by b which is either followed by not c or is followed by the end of the line: a then b, then [^c] or $.

    This kind of expression gets to be pretty unwieldy and error prone with even a short string. You could write something to generate the expressions for you, but it'd probably be easier to just use a regex implementation that supports negative lookaheads.

    *If your implementation supports non-capturing groups then you can avoid capturing extra characters.

提交回复
热议问题