我相对而言,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
1月,谢谢。当我看到我发布的内容后,立即回去尝试清理灾难,但你已经做到了。 –
嗯,'(| - |/| \\)'='([\\/- ])',如果您不需要反向引用捕获的值,则可以删除括号。还有(?:e)? = E? –
Dave/Jan, 我试过了你的建议,它根本不适合我。我甚至简单化它归结为: 〜应变= “2016年5月25日” 〜应变= fcnRegExDates(〜应变, “2016年5月25日”) MSGBOX〜应变的作品 〜应变= “05 \ 25 \ 2016” 〜应变= fcnRegExDates(strIn,“05 \\ 25 \\ 2016”)'不起作用 MsgBox strIn –