2016-05-23 14 views
-1

我相对而言,RegEx并不是很新。通过阅读许多问题和答案,我解决了我的任务,取得了一些成功,但我陷入了一个困境。查找复杂模式中的反斜杠字符

如何在复杂模式下找到反斜杠"\"。我试图将字符串中的任何和所有日期模式转换为特定的格式。我遇到的唯一问题是这种格式的日期5/22/2016(或使用反斜杠的任何变体)未找到。

我对分隔组是(|-|/|\\)我明白"\\"需要找到一个字面"\"

我自己也尝试(|-|/|c92)(|-|/|\\\\)但似乎没有任何工作。

感谢您的任何建议或想法。

Sub TestRegExDates() 
    Dim strIn As String 
    Dim lngIndex As Long 
    Dim arrDates(1) As String 
    'Goal -Convert any valid date found in a string to format MMMM, dd, yyyy" 
    arrDates(0) = "21 May 2016 * December 01, 16 * JAN-4-2016" 
    arrDates(1) = "5/21/2016 * 12/01/2016 * 1-4-2016 * 5\25\2016" 

    'It works well with dates where the month is spelled out. 
' strIn = arrDates(0) 
' strIn = fcnRegExDates(strIn, "((\b\d{4})|\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])" _ 
'        & "(|-|/|\\)(\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])(, |\s|-|/|\\)(\b\d{4}\b|\b\d{2}\b)") 
' MsgBox strIn 
    'Issue. 'It is close with numerical dates only except when "/" is used as the separator. I've used the "//" but it doesn't work in this case. 
    strIn = arrDates(1) 
    strIn = fcnRegExDates(strIn, "((\b\d{4})|\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])" _ 
          & "(|-|/|\\)(\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])(, |\s|-|/|\\)(\b\d{4}\b|\b\d{2}\b)") 
    MsgBox strIn 
    'Work around. Replace backslashes with forward slashes 
    strIn = Replace(arrDates(1), "\", "/") 
    strIn = fcnRegExDates(strIn, "((\b\d{4})|\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])" _ 
          & "(|-|/)(\bJan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\b0?[1-9]|[1-2][0-9]|3[0-1])(, |\s|-|/)(\b\d{4}\b|\b\d{2}\b)") 
    MsgBox strIn 
lbl_Exit: 
    Exit Sub 
End Sub 

Function fcnRegExDates(strIn As String, strPattern As String, Optional strDateFormat As String = "MMMM dd, yyyy") 
    Dim oRegEx As Object 
    Dim oMatchCollection As Object 
    Dim oMatch As Object 
    Dim lngIndex As Long 

    Set oRegEx = CreateObject("vbscript.regexp") 
    oRegEx.Global = True 
    With oRegEx 
    .IgnoreCase = True 
    .Pattern = strPattern 
    On Error GoTo Err_RegEx 
    If .Test(strIn) Then 
     Set oMatchCollection = .Execute(strIn) 
     fcnRegExDates = strIn 
     For Each oMatch In oMatchCollection 
     If IsDate(oMatch) Then 
      fcnRegExDates = (Replace(fcnRegExDates, oMatch, Format(oMatch, strDateFormat))) 
     End If 
     Next 
    Else 
     fcnRegExDates = strIn 
    End If 
    End With 
lbl_Exit: 
    Set oRegEx = Nothing 
    On Error GoTo 0 
    Exit Function 
Err_RegEx: 
    MsgBox "Invalid match pattern" 
    fcnRegExDates = strIn 
    Resume lbl_Exit: 
End Function 
+0

1月,谢谢。当我看到我发布的内容后,立即回去尝试清理灾难,但你已经做到了。 –

+0

嗯,'(| - |/| \\)'='([\\/- ])',如果您不需要反向引用捕获的值,则可以删除括号。还有(?:e)? = E? –

+0

Dave/Jan, 我试过了你的建议,它根本不适合我。我甚至简单化它归结为: 〜应变= “2016年5月25日” 〜应变= fcnRegExDates(〜应变, “2016年5月25日”) MSGBOX〜应变的作品 〜应变= “05 \ 25 \ 2016” 〜应变= fcnRegExDates(strIn,“05 \\ 25 \\ 2016”)'不起作用 MsgBox strIn –

回答

0

RegEx101

设置正则表达式测试为:

[0-1]{0,1}[0-9]{1}\/[0-3]{0,1}[0-9]\/[0-9]{4} 

但如果你需要限制它,你可以更具体与去年(例如预计4位数字(0000-9999)

+0

Dave/Jan, 我试过了你的建议,它根本不适用于我。我甚至简单化它归结为: 〜应变= “2016年5月25日” 〜应变= fcnRegExDates(〜应变, “2016年5月25日”) MSGBOX〜应变的作品 〜应变= “05 \ 25 \ 2016” 〜应变= fcnRegExDates(〜应变,“05 \\ \\ 25 2016”),“不工作 MSGBOX〜应变 我不知道我可能是做错了。 –

+0

我意识到这个评论看起来很糟糕,但我没有看到回复的方式并且能够格式化代码。 –

+0

Dave/Jan, 我觉得自己像个白痴。问题不在于没有找到反斜杠。问题是IsDate函数不能识别5 \ 16 \ 2016作为日期。感谢您精简模式的技巧。对不起,浪费你的时间。 –