简体   繁体   中英

c# Regex Match Exact Word in String

I have a string of text

string a = "Not Approved;Review Not Required;Terminated;Draft";
string input = "Approved";

and my input is "Approved", and it still return me false(which is what i want), but if my input is "Terminated" it still return me false(which is wrong).

Regex.Match(a, input + @"^\b").Success

Regex.match(a,@"\b" + input + @"\b".Success

Both not working like what i'm looking for

Why not just modify the regular expression a bit? Check for ';' not for '\\b'

  string a = "Not Approved;Review Not Required;Terminated;Draft";
  string input = "Approved";

  // do not forget to Escape for arbitrary input
  // true for "Terminated", false for "Approved"
  Boolean result = Regex.IsMatch(a, @"(^|;)" + Regex.Escape(input) + @"($|;)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM