.NET正则表达式的整个字符串匹配 [英] .NET Regex whole string matching
本文介绍了.NET正则表达式的整个字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们采取以下方式:。70000 @ *和970000 @ * 字符串970000@ILoveFishSticksAndTarTarSauce.com:5060 返回一个匹配70000 @ *
我明白为什么比赛,但我需要修改的正则表达式只能做一个完整的字符串匹配。
任何想法?
谢谢!
我使用.net 3.5库
下面是code:
[Microsoft.SqlServer.Server.SqlProcedure]
公共静态无效RouteRegex(字符串phoneNumber的,出字符串值)
{
如果(string.IsNullOrEmpty(phoneNumber的))
{
值=的String.Empty;
返回;
}
常量字符串strCommand =
选择RouteID,序列模式从SIPProxyConfiguration.dbo.Routes为了通过routeid,顺序递增;
使用(VAR连接=新的SqlConnection(上下文连接=真正的))
{
尝试
{
connection.Open();
使用(VAR命令=新的SqlCommand(strCommand,连接))
{
使用(VAR读卡器= Command.ExecuteReader却())
{
而(reader.Read())
{
VAR正则表达式=读卡器[2]为字符串;
如果(string.IsNullOrEmpty(正则表达式))继续;
变种routeId =读者[0];
变种序列=读者[1];
VAR匹配= Regex.Match(phoneNumber的,正则表达式);
Regex.IsMatch()
如果(match.Success!)继续;
值= routeId.ToString();
返回;
}
}
}
值=不匹配!;
}
赶上(例外前)
{
值= ex.Message;
返回;
}
}
}
解决方案
尝试使用
^ $
在你的正则表达式运算符来强制开始和结束匹配。
If we take the following patterns: 70000@.* and 970000@.* the string 970000@ILoveFishSticksAndTarTarSauce.com:5060 is returning a match to 70000@.*
I understand why it matches, but I need to modify the regex to only do a full string match.
Any ideas?
Thanks!
I am using the .Net 3.5 library
Here is the code:
[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;
}
}
}
解决方案
Try using the
^$
operators in your regex to force beginning and end matching.
这篇关于.NET正则表达式的整个字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文