【问题标题】:How to find all lines that is not matching group of regex如何查找与正则表达式组不匹配的所有行
【发布时间】:2013-06-21 17:11:37
【问题描述】:

根据这个post,我尝试使用^.*(?!http).*$查找所有不包含字符串http的行,但没有运气。

文字:

"NetworkError: 404 Not Found - http://wap-uat01.webuat.opg/finance/img/arrow.gif"
arrow.gif
GET http://wap-uat01.webuat.opg/site/brknews/xml/focus/focus_finance.xml?dummy=1372124875337
404 Not Found
        19ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/fin/xml/delay/topten/topStock_stock_up.xml?dummy=1372124875339
404 Not Found
        23ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/fin/xml/delay/topten/topStock_stock_down.xml?dummy=1372124875341
404 Not Found
        22ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/fin/xml/hotStock/fin_hotstock_utf8.xml?dummy=1372124875342
404 Not Found
        27ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/fin/xml/delay/index/u_HSI.xml?dummy=1372124875343
404 Not Found
        32ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/fin/xml/delay/index/u_HSCEI.xml?dummy=1372124875345
404 Not Found
        32ms    
xui-2.0.0.js(1221 line)
GET http://wap-uat01.webuat.opg/site/xml/polling.xml?dummy=1372124875346

对这个问题有什么想法吗?谢谢。

现场演示: http://regexr.com?35b85

【问题讨论】:

  • 空格是否必要?我很想压缩它们……我认为这不会改变基本问题,对吧?
  • @icedwater 对不起,我不太明白你的意思Is the whitespace necessary
  • 我的意思是可以删除文本行之间添加的空白行,但仍然不能改变您的问题是关于多行正则表达式的事实。例如,404 Not Found <br/> 19ms 看起来比 404 Not Found <br/><br/><br/> 19ms 更紧凑。
  • 是的,可以删除文本行。

标签: javascript regex regex-negation


【解决方案1】:

首先,要以您正在寻找的方式对其进行测试,请打开“多行”模式。 ^ 字符表示所有文本的开头,否则。 (如果没有 dotall,.* 序列将不会越过新行,但在多行模式下,您不需要 dotall。)

我认为这个表达式应该做你想要的,但它在那个页面上不起作用(我猜是因为突出显示换行符的问题):

^(?!.*?http).*$

但是,它在这里工作:

alert(
    /^(?!.*?http).*$/gm.exec('abhttpc\nq')
)

如果你不想要空行,你可以将上面的正则表达式替换为:

^(?!.*?http).+$

这确实显示了您可能正在寻找的结果:http://regexr.com?35b8h

我们的表达式之间的区别在于,您的表达式允许表达式查找任意数量的字符,而不是“http”,然后是任意数量的字符。所以,对于这条线:

"NetworkError: 404 Not Found - http://wap-uat01.webuat.opg/finance/img/arrow.gif"

...你的表情

^.*(?!http).*$

...会在不立即遇到http 的情况下,即"NetworkError: 404 Not Found -(即在空格前停止)并接受它,然后继续使用最终的http://wap-uat01.webuat.opg/finance/img/arrow.gif" 代码(即,以空格开头的代码),一直到行尾。

然而,在我修改后的代码中,它排除了在行首之后的任何地方都可以找到“http”的情况,如果不能,它会在结果中包含直到行尾的所有字符(记住(?!...) 检查实际上并没有消耗任何字符):

^(?!.*?http).+$

【讨论】:

  • 好的,谢谢——请注意,我已经给出了一个更新,解释了我们表达式之间的效果差异......
  • 抱歉,但在现场演示 jsbin.com/iyidug/1 上似乎不起作用,在控制台中得到了 [""]
  • 您可能想使用console.log(/^(?!.*?http).+$/gm.exec(t))(即带有+),因为我的代码也在抓取空行(即,从头到尾没有http,即使空)。
  • 我仍然不清楚.+.*.*.*?.*+ 在你的回答中的区别,我应该什么时候使用它们?谢谢。
猜你喜欢
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 2019-07-27
  • 1970-01-01
  • 2015-04-29
  • 2015-12-26
相关资源
最近更新 更多