Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should this regex pattern throw an exception?

Tags:

c#

regex

Should this regex pattern throw an exception? Does for me.

^\d{3}[a-z]

The error is: parsing "^\d{3}[a" - Unterminated [] set.

I feel dumb. I don't get the error. (My RegexBuddy seems okay with it.)

A little more context which I hope doesn't cloud the issue:

I am writing this for a CLR user defined function in SQL Server:

[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true)]
  public static SqlChars Match(
    SqlChars input,
    SqlString pattern,
    SqlInt32 matchNb,
    SqlString name,
    SqlBoolean compile,
    SqlBoolean ignoreCase,
    SqlBoolean multiline,
    SqlBoolean singleline
    )
  {
    if (input.IsNull || pattern.IsNull || matchNb.IsNull || name.IsNull)
      return SqlChars.Null; 

    RegexOptions options = RegexOptions.IgnorePatternWhitespace |
      (compile.Value ? RegexOptions.Compiled : 0) |
      (ignoreCase.Value ? RegexOptions.IgnoreCase : 0) |
      (multiline.Value ? RegexOptions.Multiline : 0) |
      (singleline.Value ? RegexOptions.Singleline : 0);

    Regex regex = new Regex(pattern.Value, options);
    MatchCollection matches = regex.Matches(new string(input.Value));

    if (matches.Count == 0 || matchNb.Value > (matches.Count-1))
      return SqlChars.Null;

    Match match = matches[matchNb.Value];

    int number;
    if (Int32.TryParse(name.Value, out number))
    {
      return (number > (match.Groups.Count - 1)) ? 
        SqlChars.Null :
        new SqlChars(match.Groups[number].Value);
    }
    else
    {
      return new SqlChars(match.Groups[name.Value].Value);
    }
  }

Setting it up with

CREATE FUNCTION Match(@input NVARCHAR(max), @pattern NVARCHAR(8), @matchNb INT, @name NVARCHAR(64), @compile BIT, @ignoreCase BIT, @multiline BIT, @singleline BIT)
RETURNS NVARCHAR(max)
AS EXTERNAL NAME [RegEx].[UserDefinedFunctions].[Match]
GO

And testing it with:

SELECT dbo.Match(
  N'123x45.6789' --@input
, N'^\d{3}[a-z]' --@pattern
,0 --@matchNb
,0 --@name
,0 --@compile
,1 --@ignoreCase
,0 --@multiline
,1 --@singleline
)
like image 514
alphadogg Avatar asked Jun 03 '11 19:06

alphadogg


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I create an exception in regex?

You can easily search for all characters except those in square brackets by putting a caret (^) as the first character after the left square bracket ([). To match all characters except lowercase vowels, use [^aeiou].

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


1 Answers

In your CREATE FUNCTION statement you are delcaring @pattern as NVARCHAR(8). This is truncating your pattern to 8 characters.

like image 57
Michael Ames Avatar answered Sep 22 '22 00:09

Michael Ames