所以,VBScript apparently doesn't support Lookbehind at all.
我正在寻找一个可以与 VBScript 一起使用的替代有效正则表达式。
仅供引用,我将在 HP UFT 中使用它,所以我别无选择,只能使用 VBScript (如果没有其他最简单的方法,我可能必须考虑其他选项,例如执行 Java(或其他语言)代码来自VBS)。
我想要实现的目标:
我想从给定的一堆文本中提取某些字母数字字符串。该字符串可能包括 -
、_
、.
、/
、//
、等等
我唯一知道的是,该字符串后面会跟一个特定的单词(例如DIA),并且该字符串后面会有一个空格。
这里是我可以用作替代方案的 VBS 代码片段:
此示例代码仅检索第一个匹配项。如果我找不到其他替代方案,我可以修改它。
serviceType = "DIA"
tempTxt = obj.GetROProperty("innertext")
If InStr(1, tempTxt, serviceType, 0) > 0 Then
iStartPoint = InStr(1, tempTxt, serviceType, 0) + Len(serviceType)
End If
tempTxt = LTrim(Mid(tempTxt, iStartPoint))
iStartPoint = InStr(1, tempTxt, " ", 1)
MsgBox Left(tempTxt, iStartPoint)
<小时/>
这是我正在使用的正则表达式:
(?<=DIA\s).*?(?=\s)
这是demo我已经尝试过并成功工作的内容。 我只需要找到 VBScript 替代方案。
<小时/>更新
这是我在尝试建议的正则表达式后得到的结果:
(返回值看起来不同,因为我使用不同的输入文本。)
这是我正在使用的代码:
Call RegExpMultiSearch(tempTxt, "DIA\s+(\S+)")
Public RegMatchArray
Function RegExpMultiSearch(targetString, ptrn)
'CREATE THE REGULAR EXPRESSION
Set regEx = New RegExp
regEx.Pattern = ptrn
regEx.IgnoreCase = True 'False
regEx.Global = True
'PERFORM THE SEARCH
Set Matches = regEx.Execute(targetString)
'REPORTING THE MATCHES COLLECTION
If Matches.Count = 0 Then
Actual_Res = "NO occurrence of pattern '" & ptrn & "' found in string '" & targetString & "'"
Print Actual_Res
Else
'ITERATE THROUGH THE MATCHES COLLECTION
For Each Match in Matches
'ADD TO ARRAY
ReDim Preserve arrArray(i)
arrArray(i) = Match.Value
i = i + 1
Next
Actual_Res = UBound(arrArray) - 1 & " occurrence of pattern '" & ptrn & "' found in string '" & targetString & "' successfully"
Print Actual_Res
RegMatchArray = arrArray
End If
If IsObject(regEx) Then Set regEx = Nothing End If
If IsObject(Matches) Then Set Matches = Nothing End If
End Function
最终更新
我通过使用建议的正则表达式得到了所需的结果。另外,我必须使用 SubMatches(0)
而不是 Match.Value
。
最佳答案
您可以将正则表达式重新修改为带有 capturing group 的模式这将让您只访问您需要的值:
DIA\s+(\S+)
请参阅regex demo .
请注意,您甚至不需要先行查找,因为 .*?(?=\s)
匹配除换行符之外的任何 0+ 字符,尽可能少地直到空格。当然,如果您需要检查空格,只需在模式末尾附加 \s
即可。
图案详细信息
DIA
-DIA
子字符串(如果需要整个单词匹配,则在前面加上\b
word boundary)\s+
- 1 个或多个空格(\S+)
- 第 1 组:除空白字符之外的一个或多个字符。
这是一个 VBA 测试:
Sub GetValues()
Dim rExp As Object, allMatches As Object, match As Object
Dim s As String
s = "DIA 8778680044 SVU-RMW ANNISTON SERF1450 COMMERCE BLVD ANNISTONAL DIA DS1IT-15600804-123 SVU-RMW ANNISTON2130 ROBERTS DR ANNISTONAL"
Set rExp = CreateObject("vbscript.regexp")
With rExp
.Global = True
.MultiLine = False
.Pattern = "DIA\s+(\S+)"
End With
Set allMatches = rExp.Execute(s)
For Each match In allMatches
WScript.Echo match.SubMatches.Item(0)
Next
End Sub
输出:
8778680044
DS1IT-15600804-123
关于VBScript 中的 Regex Positive Lookbehind 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46672917/