我对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 :(得分:0)
经过测试 RegEx101
将RegEx模式设置为:
[0-1]{0,1}[0-9]{1}\/[0-3]{0,1}[0-9]\/[0-9]{4}
虽然如果你需要限制它,你可以更加具体年份(例如,该年度需要4位数字(0000-9999)