我的VB.NET RegEx模式有什么问题? [英] What's wrong with my VB.NET RegEx pattern?

查看:60
本文介绍了我的VB.NET RegEx模式有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些旧的VB6代码转换为VB.NET并将其全部启动并运行,除了一个特定的RegEx模式没有找到应该匹配的匹配项。这是代码:

 ' 检查最后,第一个中间(Doe,John P.)  
pattern = [^ \\\\\\\\\\\ \\ v \,] + \,\ + + \ S +
如果 Regex.IsMatch(strName,pattern)然后
subParseLastFirst(strName)
否则
' 检查中间的第一个中间位置(John P. Doe)
pattern = ^ \S + \\\ + \ S + \ s + \ S + $
如果 Regex.IsMatch(strName,pattern)那么
subParseFirstMiddleLast(strName)
其他
' 检查最后一个(John Doe)
pattern = ^ \S + \\\ + \ S + $
如果 Regex.IsMatch(strName,pattern)然后
subParseFirstLast(strName)
否则
mblnHasError = True
mstrErrorMessage = 无法解析。
结束 如果
结束 如果
结束 如果

当我将Doe,John提供给代码时,它与第一个模式不匹配,但它在第三个模式上匹配,它将名称视为第一,最后。如果我喂Doe,John P.,第一次测试就是匹配。我应该如何编写Doe,John的模式,以及它在嵌套if中的位置?

解决方案


如果 Regex.IsMatch(strName,pattern)那么
subParseFirstMiddleLast(strName)
否则
' 检查最后一个(John Doe)
pattern = ^ \S + \s + \ S +



如果 Regex.IsMatch(strName,pattern)那么
subParseFirstLast(strName)
Else
mblnHasError = True
mstrErrorMessage =
结束 如果
结束 如果
结束 < span class =code-keyword>如果

当我将Doe,John提供给代码时,它与第一个模式不匹配,但确实如此匹配第三个,将名称视为第一个,最后一个。如果我喂Doe,John P.,第一次测试就是匹配。我应该如何编写Doe,John的模式,以及它在嵌套if中的位置?


试试这个:

'检查最后,第一个中间(Doe,John P.)
^ [a-zA-Z] +,\s [a-zA-Z] + \ [[a-zA-Z] +。

I'm converting some old VB6 code to VB.NET and have it all up and running, except that one particular RegEx pattern isn't finding a match that it should be. Here's the code:

' Check for last, first middle (Doe, John P.)
pattern = "[^ \f\n\r\t\v\,]+\,\s+\S+ "
If Regex.IsMatch(strName, pattern) Then
    subParseLastFirst(strName)
Else
    ' Check for first middle last (John P. Doe)
    pattern = "^\S+\s+\S+\s+\S+$"
    If Regex.IsMatch(strName, pattern) Then
        subParseFirstMiddleLast(strName)
    Else
        ' Check for first last (John Doe)
        pattern = "^\S+\s+\S+$"
        If Regex.IsMatch(strName, pattern) Then
            subParseFirstLast(strName)
        Else
            mblnHasError = True
            mstrErrorMessage = "Unable to parse."
        End If
    End If
End If

When I feed "Doe, John" into the code, it doesn't match on the first pattern, but it does match on the third, which treats the name as first, last. If I feed Doe, John P., the first test is a match. How should I code the pattern for Doe, John, and where should it appear in the nested if?

解决方案

" If Regex.IsMatch(strName, pattern) Then subParseFirstMiddleLast(strName) Else ' Check for first last (John Doe) pattern = "^\S+\s+\S+


" If Regex.IsMatch(strName, pattern) Then subParseFirstLast(strName) Else mblnHasError = True mstrErrorMessage = "Unable to parse." End If End If End If

When I feed "Doe, John" into the code, it doesn't match on the first pattern, but it does match on the third, which treats the name as first, last. If I feed Doe, John P., the first test is a match. How should I code the pattern for Doe, John, and where should it appear in the nested if?


Try this:

' Check for last, first middle (Doe, John P.)
^[a-zA-Z]+,\s[a-zA-Z]+\s[a-zA-Z]+.


这篇关于我的VB.NET RegEx模式有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆