4

I have the following string in a much larger config:

as-path-set DAVE-9999-CBG
as-path-set DAVE-9999-CBG
as-path-set DAVE-55555-CBG
as-path-set DAVE-44444-CBG
as-path-set DAVE-33333-CBG
as-path-set DAVE-11111-CBG
as-path-set DAVE-22222-CBG

I would like to match all of these lines except for the lines that contain 9999. I don't understand the negation regex well enough to make this work. Can someone help. The ideal output will be:

as-path-set DAVE-55555-CBG
as-path-set DAVE-44444-CBG
as-path-set DAVE-33333-CBG
as-path-set DAVE-11111-CBG
as-path-set DAVE-22222-CBG
Unihedron
  • 10,902
  • 13
  • 62
  • 72
user3133040
  • 61
  • 1
  • 6
  • Are the numbers always repeated or is `as-path-set DAVE-13547-CBG` a possible match? Is the text always the same? Is `as-path-set DAVE-99999-CBG` possible? – dee-see Dec 24 '13 at 17:31

2 Answers2

4

You can use negative lookahead:

^as-path-set DAVE-(?!9999)[0-9]+-CBG$
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

With a negative lookbehind, and knowing that all your lines end to a specific string:

.*(?<![9]{4}-CBG)$

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117
  • Might want to use `.*(?<!-[9]{4}-CBG)$` if you don't want it to match the 5 nines case. Also, why not just `.*(?<!9999-CBG)$`, rather than use the `{}` op? – Mark Dec 24 '13 at 17:59
  • 1
    @Mark OP says a line that contains 9999, so five nines also has a 4 nines in it, and about 9999 in regex, that's just a manner of coding. – revo Dec 24 '13 at 18:03
  • It wasn't clear that he actually meant 4-or-more 9s so I was just offering an alternative. The same with the alternate coding, when you're teaching someone something new and subtle like negative lookbehinds, simplify everything else. It helps. – Mark Dec 24 '13 at 18:36