HTTP头如下:1)  GET /news/ah.html HTTP/1.1   或  POST http://ss.xom/HH HTTP/1.1  
2)  HOST: ss.xom     或   HOST: ss.xom:8080 
3)  Accept:*/*
4)  Referer: http://ss.xom:8080/NN/A.HTML
5)  Content-Length:156831
…………
…………
6)  X_Forwarded_for: 127.0.0.1  或  X_Forwarded_for: 192.168.0.9,127.0.0.1,172.168.0.5
…………
…………求三个正则表达式 从以上的HTTP头中取出  1,2,6行内的主要数据。
1行 要取出 中间部分那个URI,可能是完整URL 也许是相对路径(配合HOST定位)
2行 要取出 HOST的地址可能包括端口号
6行 取出路径IP, 此行也许不存在。 
希望只在HEAD部分匹配,不要把后边其他数据给更改了,后边也许有其他自定义的HEAD值。不知道HTTPHEAD 是否必须绝对是这个样子,也许有其他变化?希望了解的朋友帮忙介绍下。

解决方案 »

  1.   

    1、((?<=GET\s*)/[a-zA-Z_\.\-]+?(?=\s))|(http:/(/[a-zA-Z_\.\-]+)+(?=\s))
    2、(?<=HOST:\s*)[a-zA-Z]+\.[a-zA-Z]+(:[0-9]{2,4})?
    3、([0-9]{1,3}\.){3}[0-9]{1,3}
      

  2.   

    改一下:
    1、((?<=GET\s*)(/[a-zA-Z_\.\-]+)+?(?=\s))|(http:/(/[a-zA-Z_\.\-]+)+(?=\s))
    2、(?<=HOST:\s*)[a-zA-Z]+\.[a-zA-Z]+(:[0-9]{2,4})? 
    3、([0-9]{1,3}\.){3}[0-9]{1,3}
      

  3.   

    哈哈 高人  不过可能你没理解 
    我不是一行一行的匹配,因为整个HEAD头是一大段文本,我并不确定要的内容在哪行出现,不过应该都是在前边,后便的大多是周边资料,这些核心信息一般都是优先发送的。
    1,2 我不知道 3好像不对吧 呵呵
      

  4.   

    看有没有点用, 测试了下像是正常? 没细看
    private void Page_Load(object sender, System.EventArgs e) 
    {
    // 在此处放置用户代码以初始化页面 string header1="GET /news/ah.html HTTP/1.1"
    +"HOST: ss.xom "
    +"Accept:*/* "
    +"Referer: http://ss.xom:8080/NN/A.HTML "
    +"Content-Length: 156831 "
    +"………… "
    +"………… "
    +"X_Forwarded_for: 127.0.0.1 "
    +"………… "
    +"………… ";

    string header2="POST http://ss.xom/HH HTTP/1.1  "
    +"HOST: ss.xom:8080 "
    +"Accept:*/* "
    +"Referer: http://ss.xom:8080/NN/A.HTML "
    +"Content-Length: 156831 "
    +"………… "
    +"………… "
    +"X_Forwarded_for: 192.168.0.9,127.0.0.1,172.168.0.5 "
    +"………… "
    +"………… "; TestHeader(header1);
    Response.Write("<hr color='blue'>\n");
    TestHeader(header2); } private void TestHeader(string strHeader)
    {
    string sxGet = @"(?<oGet>GET\s+(?<vURI>[\/\.\w]+))\s*";
    string sxPost = @"(?<oPost>POST\s+(?<vURL>http\:\/\/[\/\.\w]+))\s*";
    string sxHost = @"(?<oHost>HOST\:\s+(?<vHost>[\/\.\w]+(\:\d+)?))\s*";
    string sxAgent = @"(?<oAgent>X_Forwarded_for\:\s+(?<vIP>[\d\.\,]+))\s*";

    System.Text.RegularExpressions.Match mResult;
    mResult = System.Text.RegularExpressions.Regex.Match(strHeader, sxGet);
    if (mResult.Success)
    {
    Response.Write(mResult.Value + "<br>");
    Response.Write("vURI="+ mResult.Groups["vURI"].Value + "<hr>\n");
    }
    mResult = System.Text.RegularExpressions.Regex.Match(strHeader, sxPost);
    if (mResult.Success)
    {
    Response.Write(mResult.Value + "<br>");
    Response.Write("vURL="+ mResult.Groups["vURL"].Value + "<hr>\n");
    }
    mResult = System.Text.RegularExpressions.Regex.Match(strHeader, sxHost);
    if (mResult.Success)
    {
    Response.Write(mResult.Value + "<br>");
    Response.Write("vHost="+ mResult.Groups["vHost"].Value + "<hr>\n");
    }
    mResult = System.Text.RegularExpressions.Regex.Match(strHeader, sxAgent);
    if (mResult.Success)
    {
    Response.Write(mResult.Value + "<br>");
    Response.Write("vIP="+ mResult.Groups["vIP"].Value + "<hr>\n");
    }
    }GET /news/ah.html <br>vURI=/news/ah.html<hr>
    HOST: ss.xom <br>vHost=ss.xom<hr>
    X_Forwarded_for: 127.0.0.1 <br>vIP=127.0.0.1<hr>
    <hr color='blue'>
    POST http://ss.xom/HH <br>vURL=http://ss.xom/HH<hr>
    HOST: ss.xom:8080 <br>vHost=ss.xom:8080<hr>
    X_Forwarded_for: 192.168.0.9,127.0.0.1,172.168.0.5 <br>vIP=192.168.0.9,127.0.0.1,172.168.0.5<hr>
      

  5.   

    你是怎么得到http header信息的,俺想知道.
      

  6.   

    CQDYH的分在另外一个帖子给哈。