.NET正则表达式的整个字符串匹配

编程入门 行业动态 更新时间:2024-10-23 18:28:26
本文介绍了.NET正则表达式的整个字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我们采取以下方式:。70000 @ *和970000 @ * 字符串970000@ILoveFishSticksAndTarTarSauce:5060 返回一个匹配70000 @ *

我明白为什么比赛,但我需要修改的正则表达式只能做一个完整的字符串匹配。

任何想法?

谢谢!

我使用 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: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正则表达式的整个字符串匹配

本文发布于:2023-10-23 13:49:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!