【问题标题】:Perl Slurp Regex CapturePerl Slurp 正则表达式捕获
【发布时间】:2012-07-07 04:27:31
【问题描述】:

使用 perl,我在一个包含以下文本的大文件中“啜饮”,我试图在文件中为给定的正则表达式捕获所有正则表达式 $1 匹配项。我的正则表达式是

=~ /((GET|PUT|POST|CONNECT).*?(Content-Type: (image\/jpeg)))/sgm 

目前正在捕获粗体文本,但是,最后一次捕获正在处理行

"GET /~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1" to "Content-Type: text/html; charset=iso-8859-1" 

作为最后一次捕获的一部分,它不应该 b/c "text/html" 不等于我对 (image\/jpeg) 的正则表达式捕获。我希望能够在没有

的情况下捕获最后一次捕获
"GET /~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1" to "Content-Type: text/html; charset=iso-8859-1" being included.

感谢任何帮助,谢谢。

**GET /~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1  
Host: the.earth.li  
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Language: en-us,en;q=0.5  
Accept-Encoding: gzip, deflate  
Connection: Keep-Alive  
Content-Type: text/html; charset=iso-8859-1  
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">  
<html><head>  
\.+"  
GET /~sgtatham/putty/0.62/x86/pscp.exe HTTP/1.1  
Host: the.earth.li  
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Language: en-us,en;q=0.5  
Content-Length: 315392  
Keep-Alive: timeout=15, max=99  
Connection: Keep-Alive  
Content-Type: image/jpeg**  
Platform: Digital Engagement Platform; Version: 1.1.0.0  

【问题讨论】:

  • 我不明白你想捕捉什么。你能告诉我们你期望的输出是什么吗?

标签: regex perl capture


【解决方案1】:

您可以使用(?!pattern) 轻松做到这一点,这是一个否定的前瞻性断言。 回顾这篇文章Positive examples of positive and negative lookahead (ourcraft.wordpress.com)

正则表达式

$text =~ /
(                                 # start capture
    (?:GET|PUT|POST|CONNECT)      # start phrase
    (?:
        (?!GET|PUT|POST|CONNECT)  # make sure we'havent any these phrase
        .                         # accept any character
    )*?                           # any number of times (not greedy) 
    Content-Type:\simage\/jpeg    # end phrase
)                                 # end capture
/msx;
print $1;

所有事件

while($text =~ m/REGEXP/msxg) {

    print $1;
}

输出

GET /~sgtatham/putty/0.62/x86/pscp.exe HTTP/1.1  
Host: the.earth.li  
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101     Firefox/13.0  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Language: en-us,en;q=0.5  
Content-Length: 315392  
Keep-Alive: timeout=15, max=99  
Connection: Keep-Alive
Content-Type: image/jpeg

【讨论】:

  • 感谢 fxzuz 的发帖,这看起来很接近我想要的。此解决方案是否会匹配 slurp 文件中正则表达式捕获中的所有出现或仅匹配第一个/最后一个然后退出?我需要匹配一个大型 slurp 文件中可能出现的所有事件。谢谢
  • 是的,当然,只需将 g(全局)添加到您的正则表达式并循环处理它。我添加了这段代码来回答。
  • 谢谢,我会测试一下。再次感谢!
  • 嗨 fxzuz 我已经发布了一个关于我试图解决的 Perl 问题的新问题,希望你能看看它并帮助我。它发布在stackoverflow.com/q/12417851/1508213 我感谢您提供的任何帮助。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多