c# – Regex matching the entire string and nothing else

You probably forgot to put ^ at the start of the pattern and $ at the end of it..

..but if you’re searching for an exact string, just use == or .Equals – regex is an unnecessary performance hit.

If you mean that you have a document full of words and you want “whole word” style searching, use b before and after the pattern:

var input = "This is a test that is testing latest regex attempts";
var r = new Regex(@"btestb");
r.IsMatch(input); //true

input = "Now I attest we're testing non whole word tests";
r.IsMatch(input); //false 

The b means “word boundary” and is conceptually things like “start of input”, “whitespace”, “punctuation” etc so putting at the start and end of “test” means that if the document contains “test” it matches but it doesn’t match “attest” or “testing”

Read more here: Source link