Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 带有正则表达式的Lightswitch中存在验证错误_Regex_Visual Studio Lightswitch_Lightswitch 2013 - Fatal编程技术网

Regex 带有正则表达式的Lightswitch中存在验证错误

Regex 带有正则表达式的Lightswitch中存在验证错误,regex,visual-studio-lightswitch,lightswitch-2013,Regex,Visual Studio Lightswitch,Lightswitch 2013,我有一个属性是NIF,类似于SSN,但在西班牙,格式可以是: A0000000A 00000000A A00000000 其中A代表字母数字,0代表数字。无论如何,它必须是9个字符 使用Visual Studio 2013 LightSwitch,我尝试使用正则表达式验证它。 我为这个例子编写的正则表达式是 ^\b\w\d{7}\w\b$ 我已经在几个网站上进行了测试,到目前为止,正则表达式是有效的 但是,当我运行应用程序时,如果输入了有效数据,则该应用程序将不起作用,并且验证错误将

我有一个属性是NIF,类似于SSN,但在西班牙,格式可以是:

A0000000A  
00000000A  
A00000000
其中A代表字母数字0代表数字。无论如何,它必须是9个字符

使用Visual Studio 2013 LightSwitch,我尝试使用正则表达式验证它。
我为这个例子编写的正则表达式是

^\b\w\d{7}\w\b$
我已经在几个网站上进行了测试,到目前为止,正则表达式是有效的

但是,当我运行应用程序时,如果输入了有效数据,则该应用程序将不起作用,并且验证错误将出现在屏幕上。
以下是验证代码:

Private Sub NIF_Validate(results As EntityValidationResultsBuilder)

    Dim pattern As String = "^\b\w\d{7}\w\b$"

    If (NIF IsNot Nothing) AndAlso (Not Regex.IsMatch(pattern, NIF)) Then
            results.AddPropertyError("Check NIF")
    End If

End Sub

反斜杠是字符串中的转义字符。因此,您需要将模式字符串定义为

"^\\b\\w\\d{7}\\w\\b$"
由于
^
$
的原因,
\\b
分别
\b
在这里不需要两次


Visual Basic代码定义的正则表达式字符串解释为
^bwd{7}wb$

最后,在查看代码数小时后,我发现了错误。它位于
Regex.IsMatch
中,其中第一个参数必须是文本,第二个参数必须是模式

Regex.IsMatch(input As String, pattern As String)
谢谢你的回答。为这个愚蠢的错误感到抱歉