.NET Regex整个字符串匹配

如果我们采用以下模式:70000 @。*和970000 @。*字符串970000@ILoveFishSticksAndTarTarSauce.com:5060将匹配返回到70000 @。*

我理解为什么它匹配,但我需要修改正则表达式只进行完整的字符串匹配。

有任何想法吗?

谢谢!

我正在使用.Net 3.5库

这是代码:

[Microsoft.SqlServer.Server.SqlProcedure] public static void RouteRegex(string phoneNumber, out string value) { if (string.IsNullOrEmpty(phoneNumber)) { value = string.Empty; return; } const string strCommand = "Select RouteID,sequence,Pattern From SIPProxyConfiguration.dbo.Routes order by routeid, sequence asc"; using (var connection = new SqlConnection("context connection=true")) { try { connection.Open(); using (var command =new SqlCommand(strCommand,connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var regEx = reader[2] as string; if (string.IsNullOrEmpty(regEx)) continue; var routeId = reader[0]; var sequence = reader[1]; var match = Regex.Match(phoneNumber, regEx); Regex.IsMatch() if (!match.Success) continue; value = routeId.ToString(); return; } } } value = "No Match!"; } catch (Exception ex) { value = ex.Message; return; } } } 

尝试使用

 ^$ 

正则表达式中的运算符强制开始和结束匹配。

^开始模式,以$结束。 它们分别匹配行或字符串的开头和行或字符串的结尾。

 "^70000@.*$" matches "70000@fishsticksforlife.com" but not "970000@tatatotartar.com" "^970000@.*$" matches "970000@tatatotartar.com"