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