正则表达式量词 - 您可以使用它来查找非连续匹配吗?

时间:2014-04-17 12:53:40

标签: regex

enter code here我正在寻找发生5次的刺痛。 (这是我的字符串)我想使用警报系统来查看此字符串是否存在5次,但不是按顺序存在。如果它发生了5次,那么如果发生不那么它就不开心了。

测试文档

this is my string a bunch of text this is my string even more text this is my string more text   blah blah blah this is my string and we are almost there this is my string

If I use (.this.is.my.string){5} it works on the following data
this is my string this is my string this is my string this is my string this is my string

但是如果我在每个字符串之间有一个很大的空格(用其他文本填充)我找不到它。我已经尝试过贪婪和非贪婪,但它没有帮助。生产中的这个字符串将是搜索HTML源代码。

这是源代码的剪辑我正在搜索的字符串是div class ='proxy'我需要验证它是否存在5次。

<div class='proxy'><p class='proxyname'>DefaultRemoteProxy (version : 2.41.0)<p class='proxyid'>id : 192.168.0.180:5555, OS : VISTA</p><div     class='tabs'><ul><li class='tab' type='browsers'><a title='test slots' href='#'>Browsers</a></li><li class='tab' type='config'><a title='node     configuration' href='#'>Configuration</a></li></ul></div><div class='content'><div type='browsers' class='content_detail'><p class='protocol'     >WebDriver</p><p>v:34<img src='/grid/resources/org/openqa/grid/images/chrome.png' width='16' height='16' title='{seleniumProtocol=WebDriver,     platform=VISTA, browserName=chrome, maxInstances=1, version=34}' />
</p><p>v:5<img src='/grid/resources/org/openqa/grid/images/safari.png' width='16' height='16' title='{seleniumProtocol=WebDriver, platform=VISTA, browserName=safari, maxInstances=1, version=5}' />
</p><p>v:10<img     src='/grid/resources/org/openqa/grid/images/internet_explorer.png' width='16' height='16' title='{seleniumProtocol=WebDriver, platform=VISTA, browserName=iexplore, maxInstances=1, version=10}' />
</p><p>v:28<img src='/grid/resources/org/openqa/grid/images/firefox.png' width='16' height='16' title='{seleniumProtocol=WebDriver, platform=VISTA, browserName=firefox, maxInstances=1, version=28}' />
</p></div><div type='config' class='content_detail'><p>nodeConfig:node-config-service.json</p><p>port:5555</p><p>servlets:[] /p><p>host:192.168.0.180</p><p>cleanUpCycle:5000</p><p>browserTimeout:0</p><p>hubHost:seleniumgrid2.domain.com</p><p>registerCycle:5000</p><p>capabilityM    atcher:org.openqa.grid.internal.utils.DefaultCapabilityMatcher</p><p>newSessionWaitTimeout:-    1</p><p>url:192.168.0.180:5555</p><p>remoteHost:192.168.0.180:5555</p><p>prioritizer:null</p><p>register:true</p><p>throwOnCapabilityNotPresent:true</p><p    >nodePolling:5000</p><p>proxy:org.openqa.grid.selenium.proxy.DefaultRemoteProxy</p><p>maxSession:5</p><p>role:node</p><p>jettyMaxThreads:-    1</p><p>hubPort:4444</p><p>timeout:300000</p></div></div></div></div><div id='rightColumn'>

3 个答案:

答案 0 :(得分:1)

使用前瞻你可以做到:

^(?=(?:[\s\S]*div class='proxy'){5})[\s\S]+$

答案 1 :(得分:0)

尝试类似

的内容
 .*(this is my string.*){5}

虽然如果文字出现次数超过5次,这也可能匹配......

答案 2 :(得分:0)

可能比这更好:

((this\sis\smy\sstring\s)[^\1].*){4}this\sis\smy\sstring