C# Regex Match whole word, with special characters

前端 未结 2 1335
我寻月下人不归
我寻月下人不归 2021-01-22 18:41

I have searched through some questions but couldn\'t find the exact answer i am looking for. I have a requirement to search through large strings of text looking for keywords m

2条回答
  •  失恋的感觉
    2021-01-22 19:28

    Escape the pattern using Regex.Escape and replace the context-dependent \b word boundaries with (? / (?!\w) lookarounds:

    var rx = $@"(?

    The (? is a negative lookbehind that fails the match if there is a start of string or a non-word char immediately before the current location, and (?!\w) is a negative looahead that fails the match if there is an end of string or a non-word char immediately after the current location.

提交回复
热议问题