2011-06-03 25 views
5

¿Debería este patrón regex arrojar una excepción? Lo hace por mi¿Debería este patrón regex arrojar una excepción?

^\d{3}[a-z] 

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

me siento tonta. No entiendo el error (Mi RegexBuddy parece bien con él.)

Un poco más contexto que espero no enturbiar el tema:

estoy escribiendo esto para una función definida por el usuario CLR en 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); 
    } 
    } 

Configurándolo con

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 

Y para ello,:

SELECT dbo.Match(
    N'123x45.6789' [email protected] 
, N'^\d{3}[a-z]' [email protected] 
,0 [email protected] 
,0 [email protected] 
,0 [email protected] 
,1 [email protected] 
,0 [email protected] 
,1 [email protected] 
) 
+0

¿Podemos ver el contexto en el que lo está usando? –

+0

¿Estás tratando de unir 3 números seguidos de una letra minúscula? eg: 123a –

+0

@cory: Claro. Intentaba ser conciso. Demasiado sucinto? :) – alphadogg

Respuesta

8

En su sentencia CREATE FUNCTION, está interpretando @pattern como NVARCHAR (8). Esto está truncando su patrón a 8 caracteres.

Cuestiones relacionadas