[英]I need oracle regex to replace 2nd to 4th position characters to symbol
[英]How can I format value between 2nd and 4th underscore in the file name?
我有 VBA 代碼來將文件名捕獲到 MS Access 數據庫中的表中。
這些值如下所示:
FileName
----------------------------------------------------
WC1603992365_Michael_Cert_03-19-2019_858680723.csv
WC1603992365_John_Non-Cert_03-19-2019_858680722.csv
WC1703611403_Paul_Cert_03-27-2019_858679288.csv
每個文件名都有 4 個_
下划線,文件名的長度各不相同。
我想捕獲第二個和第三個下划線之間的值,例如:
Cert
Non-Cert
Cert
我有另一個文件下載程序,它具有正則表達式的“重命名”功能。 我設置了以下內容:
Source file Name: (.*)\_(.*)\_(.*)\_(.*)\_\-(.*)\.(.*)
New File Name: \5.\6
在本例中,我將文件名的第 5 部分移到前面,並添加文件擴展名。
例如, WC1603992365_Michael_Cert_03-19-2019_858680723.csv
將在文件夾中另存為858680723.csv
。
有沒有辦法可以使用 RegEx 捕獲文件名的第三部分,並將值保存在字段中?
我嘗試了 VBA 代碼,並搜索了 SQL 示例,但沒有找到。
因為文件名長度不固定,所以不能使用LEFT
或RIGHT
...
先感謝您。
一種可能的解決方案是使用 VBA Split
function 將字符串拆分為使用下划線作為分隔符的字符串數組,然后返回該數組中索引 2 處的項目。
例如,您可以定義一個 VBA function,如下所示,駐留在公共模塊中:
Function StringElement(strStr, intIdx As Integer) As String
Dim strArr() As String
strArr = Split(Nz(strStr, ""), "_")
If intIdx <= UBound(strArr) Then StringElement = strArr(intIdx)
End Function
在這里,我將參數strStr
定義為 Variant,以便您可以將Null
值傳遞給它而不會出錯。
如果提供了Null
值,或者如果提供的索引超出了使用下划線拆分字符串返回的數組的邊界,則 function 將返回一個空字符串。
然后,您可以從 SQL 語句中調用上述 function :
select StringElement(t.Filename, 2) from Filenames t
在這里,我假設您的表名為Filenames
- 將其更改為適合。
這是我完成的工作代碼。 感謝您分享您的答案。
Public Function getSourceFiles()
Dim rs As Recordset
Dim strFile As String
Dim strPath As String
Dim newFileName As String
Dim FirstFileName As String
Dim newPathFileName As String
Dim RecSeq1 As Integer
Dim RecSeq2 As Integer
Dim FileName2 As String
Dim WrdArrat() As String
RecSeq1 = 0
Set rs = CurrentDb.OpenRecordset("tcsvFileNames", dbOpenDynaset) 'open a recordset
strPath = "c:\in\RegEx\"
strFile = Dir(strPath, vbNormal)
Do 'Loop through the balance of files
RecSeq1 = RecSeq1 + 1
If strFile = "" Then 'If no file, exit function
GoTo ExitHere
End If
FirstFileName = strPath & strFile
newFileName = strFile
newPathFileName = strPath & newFileName
FileName2 = strFile
Dim SubStrings() As String
SubStrings = Split(FileName2, "_")
Debug.Print SubStrings(2)
rs.AddNew
rs!FileName = strFile
rs!FileName68 = newFileName 'assign new files name max 68 characters
rs!Decision = SubStrings(2) 'extract the value after the 3rd underscore, and add it to Decision Field
rs.Update
Name FirstFileName As newPathFileName
strFile = Dir()
Loop
ExitHere:
Set rs = Nothing
MsgBox ("Directory list is complete.")
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.