3

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;
            }
        }

    }
Wjdavis5
  • 3,952
  • 7
  • 35
  • 63

2 Answers2

6

Try using the

^$ 

operators in your regex to force beginning and end matching.

Matthew Shea
  • 567
  • 5
  • 14
  • Great thanks! I tried this in Notepad++ (which as known regex limitations) and it didnt work. I'll throw it into the CLR SPROC tomorrow and try it out. Are there any known limitations to the .NET Regex Library? – Wjdavis5 Mar 07 '13 at 01:56
  • Not that I know of. I use these operators regularly. Also, I use http://regexpal.com/ to test things, typically and it works with .NET – Matthew Shea Mar 07 '13 at 05:00
4

Start the pattern with ^ and end with $. These match the start of the line or string and end of the line or string respectively.

"^70000@.*$" matches "70000@fishsticksforlife.com" but not "970000@tatatotartar.com"
"^970000@.*$" matches "970000@tatatotartar.com"
Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80