duanji1043 2012-05-11 03:05
浏览 20
已采纳

对大型正则表达式使用延迟评估(而不仅仅是。*?)

Using the follow regex:

\[\w* \w* \d{2} [\w:]* \d{4}\] \[error\] \[client .*?\] .*? Using HTTP not .*?<br /> 

I get the following results (where yellow boxes indicate a match):

Sublime Text 2

Raw Text: http://pastebin.com/vSi0mLGv

The bottom two sections are correct. I want all sections that contain: &lt;&lt;&lt;NOTICE&gt;&gt;&gt; Non-Prod Server: Using HTTP not HTTP/S

The top section however, contains the correct string (similar to the bottom two), but also comes with a whole other chunk that I do not want:

[Thu May 10 17:43:48 2012] [error] [client ::1] Current Name:
DashboardBar_projAnnualReview200, referer: http://
localhost/test/pages/TestPage.php<br />`

I know this comes down to regex being greedy, but how can I go about making it do a lazy evaluation for the <br />, if that's even the right way to go about it. I've tried (<br />)*? and others to no avail.


Other Information: I am using Sublime Text 2, and performing a regex search if anyone wanted to recreate the image.

  • 写回答

2条回答 默认 最新

  • duanqu9279 2012-05-11 03:36
    关注

    Greediness is not the problem, eagerness is. The regex engine starts trying to match at the earliest opportunity, and it doesn't give up until every possibility has been exhausted. Making quantifiers non-greedy doesn't change that, it just changes the order in which the possibilities are tried.

    It's not the * in .* that's causing your problem, it's the .. You need to use something more restrictive, because it's allowing the match to start too early. This regex works as desired because I've replaced the .*? with [^][]*, which matches any characters except ] or [:

    \[\w* \w* \d{2} [\w:]* \d{4}\] \[error\] \[client [^][]*\] [^][]* Using HTTP not .*?<br />
    

    I don't know what regex flavor SublimeText uses, so you may need to escape the square brackets inside the character class:

    \[\w* \w* \d{2} [\w:]* \d{4}\] \[error\] \[client [^\]\[]*\] [^\]\[]* Using HTTP not .*?<br />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100