Home > OS >  Filter out exact words from part of string - java regex
Filter out exact words from part of string - java regex

Time:12-22

I want to match all lines that start with fn- except ones that use specific words after the hyphen.

match:

fn-bar
fn-foo
fn-foobarb

dont match (foobar and fubar are my exact negative filters):

fn-foobar
fn-fubar

xn-blah

So far I have:

(fn)-(?!(fubar|foobar)$)

which does not match the whole line

CodePudding user response:

You can use the word delimiter \b to avoid matching the "foobar" and "fubar" words. Instead in order to avoid whole line issue, it's sufficient to antepose the negation of the full phrases you don't want to match.

(?!fn-foobar\b|fn-fubar\b)fn-.*

Check the demo here.

  • Related