简体   繁体   中英

Filter out exact words from part of string - java regex

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM