dscizpq790832708 2011-08-16 20:38
浏览 36
已采纳

尝试匹配数字时出现PHP正则表达式问题

I have an issue with regular expressions in PHP.

I'm trying to find a match for some header data that I received via CURL. However, my regex is matching two lines, instead of one.

Below is my regex pattern:

/^(http\/\d\.\d)\s+(100)\s+([\w\-\s\/\'\"\(\)\\\.]+)$/im

And here is the string I'm trying match:

HTTP/1.1 100 Continue

HTTP/1.1 201 Created
Content-Length: 118
Content-Type: text/html; charset=UTF-8
Etag: 8c59b7e37f672374c61245c8115f53d0
Last-Modified: Tue, 16 Aug 2011 20:24:10 GMT
X-Trans-Id: txd6cbafcec90d4e30b2a108ff3157c1b1
Date: Tue, 16 Aug 2011 20:24:10 GMT

It's supposed to match the first line but I'm getting a match for both 100 and 201. I can't understand why it's doing this.

I'm using preg_match by the way.

Any ideas as to how I can solve this? Thanks.

  • 写回答

2条回答 默认 最新

  • doufu9145 2011-08-16 20:49
    关注

    Update:

    The multiline modifier is orthogonal to the real cause. Your problem is that the character class definition (That between the square brackets) includes \s, which matches any whitespace character, including linebreaks. Replace it with an actual space in this case, to match just that.

    As an aside, if you have access to install extensions on your server, you might want to use the pecl_http extension for parsing http, rather than doing it by hand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog