首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl Slurp正则表达式捕获

Perl Slurp正则表达式捕获
EN

Stack Overflow用户
提问于 2012-07-07 12:27:31
回答 1查看 652关注 0票数 3

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

代码语言:javascript
复制
=~ /((GET|PUT|POST|CONNECT).*?(Content-Type: (image\/jpeg)))/sgm 

当前以粗体显示的文本正在被捕获,但是,最后一次捕获正在处理行

代码语言:javascript
复制
"GET /~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1" to "Content-Type: text/html; charset=iso-8859-1" 

作为最后捕获的一部分,它不应该是b/c“文本/html”不等于我的正则表达式捕获的(image\/jpeg)。我希望能够捕获最后一个捕获,而不需要

代码语言:javascript
复制
"GET /~sgtatham/putty/latest/x86/pscp.exe HTTP/1.1" to "Content-Type: text/html; charset=iso-8859-1" being included.

感谢您的帮助,谢谢。

代码语言:javascript
复制
**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  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-07 14:29:21

你可以用(?!pattern)很容易做到这一点,这是一个消极的前瞻性断言。有关概述,请阅读本文Positive examples of positive and negative lookahead (ourcraft.wordpress.com)

正则表达式

代码语言:javascript
复制
$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;

所有实例

代码语言:javascript
复制
while($text =~ m/REGEXP/msxg) {

    print $1;
}

输出

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11372265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档