Reputation: 12930
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
)
Upvotes: 6
Views: 724
Reputation: 2617
In your CREATE FUNCTION statement you are delcaring @pattern as NVARCHAR(8). This is truncating your pattern to 8 characters.
Upvotes: 8