percentum
percentum

Reputation: 153

C# Regex Match whole word, with special characters

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 matches. I was using IndexOf, however, i require to find whole word matches e.g. if i search for Java, but the text contains JavaScript, it shouldn't match. This works fine using \b{pattern}\b, but if i search for something like C#, then it doesn't work.

Below is a few examples of text strings that i am searching through:

languages include Java,JavaScript,MySql,C#
languages include Java/JavaScript/MySql/C#
languages include Java, JavaScript, MySql, C#

Obviously the issue is with the special character '#'; so this also doesn't work when searching for C++.

Upvotes: 2

Views: 3218

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

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.

Upvotes: 2

Dan
Dan

Reputation: 10786

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

Upvotes: 1

Related Questions