简体   繁体   中英

regex filter out words wrapped in certain characters

I am trying to use regex in c# to handle a scenario where I want to find all instances of a word that are not wrapped in certain characters. A quick example is below when trying to highlight the word "high"

highlight
[+highlight
highlight+]
[+highlight some more+]
[+highlight+]

I was trying to use negative look ahead and behind but this doesn't appear to play all that nice. Something like this

(?<!\[[\+|-])\w*high\w*(?![\+|-]\])

The outcome I am after is the word "high" would be found in all but the last scenario.

Try this pattern:

(?<=^|\s)(?<!\[[+|-])\w*high\w*|\w*high\w*(?![+|-]\])(?=$|\s)

Regex101

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