【问题标题】:Logical OR not working in regular expression逻辑或在正则表达式中不起作用
【发布时间】:2014-07-19 17:49:30
【问题描述】:

我正在处理一个大日志文件,其条目如下:

-- "GET <b>/fss-w3-mtpage.php</b> HTTP/1.1" 200 0.084 41 "-" "c110bc/1.0" 127.0.0.1:25001  0.084

-- "GET <b>/m/firstpage/Services/getAll</b>?ids=ABCVDFDS,ASDASBDB,ASDBSA&requestId=091fa2b4-643e-4473-b6d8-40210b775dcf HTTP/1.1" 200

-- POST <b>/lastpage/Services/getAll</b>?ids=ABCVDFDS,ASDASBDB,ASDBSA&requestId=091fa2b4-643e-4473-b6d8-40210b775dcf HTTP/1.1" 200

我想提取上面示例中加粗的部分。这是我为上述内容编写的正则表达式

.*(POST|GET)\s+(([^\?]+)|([^\s])) 

我想获取GETPOST 之后的部分,直到第一次出现空格' ' 或问号'?'

问题
正则表达式后面部分的逻辑 OR 不起作用。 如果我只使用

.*(POST|GET)\s+([^\?]+)    

我得到了正确的部分,即从 GET 或 POST 到第一个问号 '?'。同样,如果我使用

.*(POST|GET)\s+([^\s]+)    

我得到了正确的部分,即从 GET 或 POST 到第一个空格 ' ')。

谁能告诉我哪里错了?

【问题讨论】:

    标签: regex regex-alternation


    【解决方案1】:

    [^\?]+ 我得到正确的部分直到第一个问号,
    有了[^\s]+,我得到了正确的部分,直到第一个空格

    因为这些字符类别意味着:所有不是问号的字符,或者:所有不是空格的字符。

    要组合它们,你想说:所有既不是问号也不是空格的字符:

    [^?\s]+
    

    使用您使用的 OR,它只是尝试了第一个([^\?]+ - 包括空格),它成功了,如果第一个不起作用,它会回溯并尝试 [^\s]+(包括问号) .

    【讨论】:

      【解决方案2】:

      从索引 2 中获取匹配组

      \b(POST|GET)\s+([^?\s]+)
      

      这里是DEMO

      模式说明:

        \b                       the word boundary
      
        (                        group and capture to \1:
          POST                     'POST'
         |                        OR
          GET                      'GET'
        )                        end of \1
      
        \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or more times)
      
        (                        group and capture to \2:
      
          [^?\s]+                  any character except: '?', whitespace
                                   (\n, \r, \t, \f, and " ") (1 or more times)
      
        )                        end of \2
      

      【讨论】:

        【解决方案3】:

        下面的正则表达式将仅匹配紧跟在GETPOST 之后的字符串,后跟空格或? 符号。

        (?<=GET |POST )\s*.*?(?= |\?)
        

        DEMO

        您可以使用捕获组(),以捕获匹配的字符串。

        (?<=GET |POST )\s*(.*?)(?= |\?)
        

        DEMO

        说明:

        (?<=                     look behind to see if there is:
          GET                      'GET '
         |                        OR
          POST                     'POST '
        )                        end of look-behind
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                                 more times)
        (                        group and capture to \1:
          .*?                      any character except \n (0 or more
                                   times)
        )                        end of \1
        (?=                      look ahead to see if there is:
                                   ' '
         |                        OR
          \?                       '?'
        )                        end of look-ahead
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多