lev*_*ski 5 regex string vba extract extraction
我需要从具有多个条件的字符串中提取数字。
以下是几个示例:
从RE:Markitwire:120432889:Mx:24,693,059我需要得到24693059
从自动回复:Auftrag zurÜbertragungIRD Ref-Nr。MMX_23497152需要获取23497152
现在,我正在使用regexextract函数(在此网站上找到),该函数提取以2开头的8位数字。但是,它也将从表达式TGF00023242526提取数字,这是不正确的。而且,我不知道如何在代码中添加其他条件。
=RegexExtract(A11, ""(2\d{7})\b"", ", ")
Run Code Online (Sandbox Code Playgroud)
先感谢您。
Function RegexExtract(ByVal text As String, _
ByVal extract_what As String, _
Optional seperator As String = "") As String
Dim i As Long, j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = extract_what
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(i).SubMatches.Count - 1
result = result & seperator & allMatches.Item(i).SubMatches.Item(j)
Next
Next
If Len(result) <> 0 Then
result = Right(result, Len(result) - Len(seperator))
End If
RegexExtract = result
End Function
Run Code Online (Sandbox Code Playgroud)
您可以在您拥有的模式之前使用非捕获组创建自定义边界:
(?:[\D0]|^)(2\d{7})\b
^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
该(?:[\D0]|^)部分与字符串 ( ) 的非数字 ( \D) 或0( |) 开头匹配^。
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |