2017-10-06 47 views
4

我一直在想出一种方法来解决我正在通过PCAP文件搜索的情况。在HTTP OK响应之后,我正在寻找标题“Content-Type:。*”。然而,在这样一个例子:正则表达式跳过类似的结果

HTTP/1.1 200 OK 
date: 
asdf 
X-Content-Type: aadsf 
Content-Type: application/json 
more: stuff 

HTTP/1.1 200 OK 
date: 
asdf 
X-Content-Type: aadsf 
Content-Type: application/json 
more: stuff 

我现在的正则表达式"HTTP\/1.1 200 OK[\s\S]*?Content-Type:.*"X-Content-Type: aadsf停止捕获组。我的意图是正则表达式捕获组去Content-Type: application/json

任何正则表达式向导,可以给我一些指针?

+0

您在这里使用哪种语言? – Jan

回答

2

一个PCRE正则表达式没有,你可以使用lookarounds是

regex demo。如果你想让它更有效率,replace the first .+ with .++。它可以很容易地与捕获组使用重写,说(CR)LF结束:

^HTTP.*(?:\r?\n.+)*?\r?\nContent-Type:\s*(.+) 

注意m多修改,使线路的^比赛开始可能仍然是必要的。

详细

  • ^ - 一个子
  • .* - - 行
  • (?:\R.+)*?的其余部分 - 任何0+,尽可能少的线路
  • HTTP开始,换行符(\R\r?\n)的序列后跟1个或多个换行符以外的字符
  • \R - 换行符
  • Content-Type: - 文字串
  • \s* - 0+空格
  • \K - 匹配复位操作者丢弃来自当前匹配值为止匹配所有文本
  • .+ - 1或除了换行符以外,还有更多的字符。
+0

非常好(+1),与我的相比,这大大减少了步骤。 – Jan

0

这里是正则表达式^((?:X-)?Content-Type):(.*)$它捕获两种内容类型。或者,如果您希望它在一个附加内容类型后停止,只需在Content-Type之前附加\n(换行符)。

1

你可以使用

^HTTP    # match HTTP at the start of the line 
(?s:(?!^$).)+? # anything lazily, do not overrun an empty newline 
^Content-Type:\s* # Content-Type: at the start of a line 
(?P<type>.+)  # capture the type 

a demo on regex101.com