C# Regex Match whole word, with special characters

前端 未结 2 1398
我寻月下人不归
我寻月下人不归 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

    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,.;]|$)
    

提交回复
热议问题