这个问题似乎是Find specific text in a string ...的延续
您可以将 VBA 函数用作工作表函数。
如果您将以下代码添加到您从我之前的答案中添加的代码中:
Public Function RegExMatch(strInput As String, strRegEx As String, Optional MatchNo As Long = 0, Optional FirstIDX As Long, Optional Lgth As Long, Optional bIgnoreCase As Boolean = True, Optional bMultiLine As Boolean = False) As String
Dim RegExp As VBScript_RegExp_55.RegExp, Matches As VBScript_RegExp_55.MatchCollection
Set RegExp = New VBScript_RegExp_55.RegExp
With RegExp
.Global = True
.MultiLine = bMultiLine
.IgnoreCase = bIgnoreCase
.Pattern = strRegEx
End With
If RegExp.test(strInput) Then
Set Matches = RegExp.Execute(strInput)
If MatchNo > Matches.Count - 1 Then
RegExMatch = ""
Else
RegExMatch = Matches(MatchNo).value
FirstIDX = Matches(MatchNo).FirstIndex
Lgth = Matches(MatchNo).Length
End If
Else
RegExMatch = ""
End If
Set RegExp = Nothing
End Function
...然后你可以有一个公式:
=IF(RegEx(A1,"[A-Z]{2}-\d\d"), RegExMatch(A1,"[A-Z]{2}0{5}\d{6,8}"),"")
您可能想查看此处的参考部分,以获取有关正则表达式的更多有用信息。