Regular Expression Word Boundary Not Working If Using A Variable In C#
In this scenario, I have to match an exact state abbreviation (QLD) that is Queensland. I declared an array containing state constant values. Normally this would work without using a string variable in C#:
However, this won't work:
The solution is to put an @ sign on both "\b" of the expression:
Regex.IsMatch(address, @"\bQLD\b")
if (Regex.IsMatch(address, @"\b" + str + "\b"))
string[] states = new string[]{"ACT","NSW","QLD"}; foreach (string str in states) { if (Regex.IsMatch(address, @"\b" + str + @"\b")) { state= str; address = address.Replace(str, "").Trim(); break; } }
Nice.
ReplyDelete