C# Regex Match whole word, with special characters

前端 未结 2 1334
我寻月下人不归
我寻月下人不归 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) / (?!\w) lookarounds:

    var rx = $@"(?<!\w){Regex.Escape(pattern)}(?!\w)";
    

    The (?<!\w) 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.

    0 讨论(0)
  • 2021-01-22 19:28

    Yeah, this is because there isn't a word boundary (a \b) after the #, because # isn't a "word" character. You could use a regular expression like the following, which searches for a character that isn't a part of a language name [^a-zA-Z+#] after the language:

    \b{pattern}[^a-zA-Z+#]
    

    Or, if you believe you can list all of the possible characters that aren't part of a language name (for example, whitespace, ,, ., and ;):

    [\s,.;]{pattern}[\s,.;]
    

    Alternately, if it is possible that a language name is at the very end of a string (depending on what you're getting the data from), you might need to also match the end of the string $ in addition to the separators, or similarly, the beginning of the string ^.

    [\s,.;]{pattern}(?:[\s,.;]|$)
    
    0 讨论(0)
提交回复
热议问题