- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,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/
我正在寻找一个正则表达式来在 : 字符上拆分用户提供的字符串,但当用户转义冒号 \: 或者它是 url 的一部分时则不会,例如https://stackoverflow..。在 javascript
我试图让正则表达式以负向后视的方式捕获一些数据,这样如果某个字符串在它前面,它就不会匹配。我知道有两种基本格式,但都不起作用。 我在搜索应用程序中执行此操作,无法使用 java 进行扩充,因此解决方案
我有这个目标: 给定字符串:"Part1-part2-part3-part4-part5" 在第二次出现“-”时拆分它, 所以我期待一个数组 [ "Part1-part2", "part3-part4
我正在为此寻找替代方案: (?x[1]); 编辑: 要使用您当前的规范从 str.123 中删除 3,请使用相同的捕获 方法:捕获您需要的内容并恢复使用替换模式中的 $n 反向引用在结果中捕获的文本,
我正在为此寻找替代方案: (?x[1]); 编辑: 要使用您当前的规范从 str.123 中删除 3,请使用相同的捕获 方法:捕获您需要的内容并恢复使用替换模式中的 $n 反向引用在结果中捕获的文本,
这个问题在这里已经有了答案: Javascript Regex Lookbehind Alternative (2 个答案) 关闭 4 年前。 我正在匹配下面描述的三种模式,它们都是独立的。按照超链
在令人敬畏的正则表达式模块 (https://pypi.python.org/pypi/regex) 的 pypi 页面中指出\G 可以“在负的可变长度后视中使用以限制后视的距离”。非常有趣,但该页面
我正在尝试使用 tidyr 中的 extract 函数进行一些模式匹配。我已经在正则表达式练习网站上测试了我的正则表达式,该模式似乎有效,而且我正在使用 lookbehind assertion。 我
我需要在我的语料库中搜索诸如game 或shame 之类的词,但我想指定搜索以排除三个字符串 a game/a shame or , A game/A shame and a/an/A/An WORD
我试图在公式中替换所有缺少前面零的 float 。例如: “4+.5”应该变成:“4+0.5” 现在我读到 JavaScript 不支持向后看,那么我该如何实现呢?当前面有数字时,以下代码也会替换:
Javascript 中的 lookbehind regexps 有几种(有时是棘手的)解决方案。但是,这是最简单的方法,如果我需要一个零宽度! 使用全局搜索查看表达式,这可能会重叠。例如。使用 /(
更新的问题 假设字符串 "?foo=bar&nonfoo=bar&foo=bar" ,在这种情况下我需要捕获: foo=bar foo=foo 我用 Perl 做的, see here . 但是Jav
我需要捕获以 # 开头的所有行JavaScript 中的字符。我尝试使用类似以下正则表达式的内容,但事实证明 JavaScript 不支持积极的后向断言 (?<=) . /(?<=\n)\#[^\n]
我想将某些文件类型(例如“.txt”)与不以特定子字符串结尾的非空根名称(例如“-bad”)匹配。有了负后视支持,解决方案很简单: /.(? ((regex.test(input) === expec
我在 .net 中有一个字符串。 Para 1Para 2Para 3Para 4 现在,我只想获取标签 p 内的文本(Para 1、Para 2、Para 3、Para4)。 我使用了以下正则表达式
我有一个包含多个关键字的字符串。我想将字符串拆分为这些关键字的列表(但保留关键字,因为它们确定了以下数据的含义) 以下面的字符串为例: test_string = "ªttypmp3pfilfDjTu
如何让lookbehind变得贪婪? 在这种情况下,我希望后向处理消耗 : if is 存在。 m = Regex.Match("From: John", @"(?i)(?<=from:)....")
我想捕捉不带空格的单词。 var paragraphy="Apple banana kişiler ki örnek foo."; var word="kişiler"; var regex = ne
我正在尝试匹配以 .xsd 结尾但不以 form.xsd 结尾的字符串列表,我使用以下正则表达式: ArrayList files = new ArrayList(); files.add("/aba
此问题特定于 Java 7/8。 使用量词的相当复杂的正则表达式在像这样的后向断言中是被禁止的: (?<=(a|b*)*)bc 因为它会导致运行时异常并显示如下消息: look-behind grou
我是一名优秀的程序员,十分优秀!