我提取了网络驱动器上的所有文件,其中一些文件名是部件号,部件号格式为0000-000000-00
。现在,在此文件的600,000多个路径名中,我试图找出如何从路径名中提取零件号。我认为mid
公式可能会起作用,但是我对如何告诉它查找具有部分#格式0000-000000-00
的内容并从路径中仅提取那14个字符感到迷惑?
输入看起来像这样
c:\users\stuff\folder_name\1234-000001-01_ baskets_1.pdf
c:\users\stuff\folder_name\1234-000001-02_ baskets_2.pdf
c:\users\stuff\folder_name\1234-000001-03_ baskets_3.pdf
c:\users\stuff\folder_name\1234-000030-01_ tree_30.pdf
c:\users\stuff\folder_name\random text_1234-000030-02_ tree_30.pdf
c:\users\stuff\folder_name\more random stuff_1234-000030-02_ tree_30.pdf
我希望得到的输出
1234-000001-01
1234-000001-02
1234-000001-03
1234-000030-01
答案 0 :(得分:1)
既然您有可以利用的模式,请使用此模式:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim(DisplayNameClaimType, FirstName));
return userIdentity;
}
查找模式的开头,然后返回14个字符。
答案 1 :(得分:1)
您需要一个公式,但也可以使用UDF来应用正则表达式来获取模式(在这种情况下,有些过大但值得注意):
Option Explicit
Public Sub GetCustomString()
Dim i As Long, tests()
tests = Array("c:\users\stuff\folder_name\1234-000001-01_ baskets_1.pdf", _
"c:\users\stuff\folder_name\1234-000001-02_ baskets_2.pdf", _
"c:\users\stuff\folder_name\1234-000001-03_ baskets_3.pdf", _
"c:\users\stuff\folder_name\1234-000030-01_ tree_30.pdf", _
"c:\users\stuff\folder_name\random text_1234-000030-02_ tree_30.pdf", _
"c:\users\stuff\folder_name\more random stuff_1234-000030-02_ tree_30.pdf")
For i = LBound(tests) To UBound(tests)
Debug.Print GetString(tests(i))
Next
End Sub
Public Function GetString(ByVal inputString As String) As String
Dim arr() As String, i As Long, matches As Object, re As Object
Set re = CreateObject("VBScript.RegExp")
With re
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "\d{4}-\d{6}-\d{2}"
If .test(inputString) Then
GetString = .Execute(inputString)(0)
Else
GetString = vbNullString
End If
End With
End Function
在工作表中使用UDF:
模式:\d{4}-\d{6}-\d{2}
说明:
\ d {4}匹配一个数字(等于[0-9])
{4}量词-精确匹配4次
“-”与字符匹配-从字面上(区分大小写)
\ d {6}匹配一个数字(等于[0-9])
{6}量词-精确匹配6次
“-”与字符匹配-从字面上(区分大小写)
\ d {2}匹配一个数字(等于[0-9])
{2}量词-精确匹配2次
全局模式标志: g修饰符:全局。所有比赛(第一次比赛后不返回) m修饰符:多行。导致^和$匹配每行的开头/结尾(不仅是字符串的开头/结尾)