下午好,
我正在努力解决这个问题(VB.Net)并需要一些帮助。
我在目录中有大约800个文件,我想获取文件名并从中提取客户端编号。
目录中文件的样子如下:
Client 11111 Proposal 47241 inputted.msg
Client 22222 Proposal 88754 inputted.msg
Interest Portfolio 5514720 inputted for Client 33333.msg
Investment Proposal 0987654 inputted for Client 34343.msg
Investment Proposal 1234567 inputted for Client 33333.msg
Investment Proposal 7456781 inputted for Client 66666.msg
运行代码时,应该得到以下结果:
11111
22222
33333
34343
33333
66666
以下是我正在使用的代码:
Dim path = txtWatchPath.Text
Dim files = Directory.GetFiles(path, "*.msg")
Dim expr = New Regex("\bClient\b\s\d{5}")
For Each file In files
If expr.IsMatch(file) Then
lsbxLog.Items.Add(file)
End If
Next
但是此代码的输出显示如下:
C:\Temp\Client 11111 Proposal 47241 inputted.msg
C:\Temp\Client 22222 Proposal 88754 inputted.msg
C:\Temp\Interest Portfolio 5514720 inputted for Client 33333.msg
C:\Temp\Investment Proposal 0987654 inputted for Client 34343.msg
C:\Temp\Investment Proposal 1234567 inputted for Client 33333.msg
C:\Temp\Investment Proposal 7456781 inputted for Client 66666.msg
请有人帮助我,我只需要文件名中的客户编号。
亲切的问候, 甲
答案 0 :(得分:0)
假设客户端的长度始终为5:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As New List(Of String)
filenames.Add("Client 11111 Proposal 47241 inputted.msg")
filenames.Add("Client 22222 Proposal 88754 inputted.msg")
filenames.Add("Interest Portfolio 5514720 inputted for Client 33333.msg")
filenames.Add("Investment Proposal 0987654 inputted for Client 34343.msg")
For Each fileName In filenames
MessageBox.Show(GetClientName(fileName))
Next fileName
End Sub
Private Function GetClientName(fileName As String) As String
Dim leadingString = "Client "
Dim startPos = fileName.IndexOf(leadingString) + leadingString.Length
Dim parsedString = fileName.Substring(startPos, 5)
Return parsedString
End Function