Regex Perl Slurp正则表达式捕获

Regex Perl Slurp正则表达式捕获,regex,perl,capture,Regex,Perl,Capture,使用perl,我在一个包含以下文本的大文件中“slurped”,并尝试捕获文件中给定正则表达式的所有正则表达式$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

使用perl,我在一个包含以下文本的大文件中“slurped”,并尝试捕获文件中给定正则表达式的所有正则表达式
$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  
**GET/~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1
主持人:地球·李
用户代理:Mozilla/5.0(Macintosh;英特尔Mac OS X 10.6;rv:13.0)Gecko/20100101 Firefox/13.0
接受:text/html、application/xhtml+xml、application/xml;q=0.9,*/*;q=0.8
接受语言:en-us,en;q=0.5
接受编码:gzip,deflate
连接:保持活力
内容类型:text/html;字符集=iso-8859-1
\.+"  
GET/~sgtatham/putty/0.62/x86/pscp.exe HTTP/1.1
主持人:地球·李
用户代理:Mozilla/5.0(Macintosh;英特尔Mac OS X 10.6;rv:13.0)Gecko/20100101 Firefox/13.0
接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
接受语言:en-us,en;q=0.5
内容长度:315392
保持活动状态:超时=15,最大=99
连接:保持活力
内容类型:图像/jpeg**
平台:数字交战平台;版本:1.1.0.0

您可以使用
(?!pattern)
轻松实现,这是一个消极的前瞻性断言。 重述一下,请阅读本文

正则表达式

$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(全局)到您的regexp并在循环中处理它。我已经添加了这段代码来回答。谢谢您,我将对此进行测试。再次感谢!嗨,fxzuz,我已经发布了一个关于我试图解决的Perl问题的新问题,希望您能看一下并帮助我解决。它发布在我感谢您提供的任何帮助。谢谢