Regex 逻辑或不在正则表达式中工作
我正在处理一个大日志文件,其条目如下:Regex 逻辑或不在正则表达式中工作,regex,regex-alternation,Regex,Regex Alternation,我正在处理一个大日志文件,其条目如下: -- "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-b6
-- "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
我想得到get
或POST
之后的部分,直到第一次出现空格“
或问号?”
问题正则表达式后面部分中的逻辑OR无效。 如果我只用
.*(POST|GET)\s+([^\?]+)
我得到了正确的部分,即从GET或POST到第一个问号?'
。如果我使用
.*(POST|GET)\s+([^\s]+)
我得到了正确的部分,即从GET或POST到第一个空格'
)
有谁能告诉我哪里错了吗?从索引2中获取匹配的组
\b(POST|GET)\s+([^?\s]+)
这是
模式说明:
\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
从索引2中获取匹配的组
\b(POST|GET)\s+([^?\s]+)
这是
模式说明:
\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
使用[^\?]+
我得到了正确的部分,直到第一个问号,使用
[^\s]+
我得到了正确的部分,直到第一个空格
因为这些字符类的意思是:所有没有问号的字符,或者:所有没有空格的字符
要组合它们,您需要说:所有既不是问号也不是空格的字符:
[^?\s]+
对于您使用过的OR,它只是尝试了第一个([^\?]+
-包括空格),成功了,如果第一个不起作用,它会回溯并尝试[^\s]+
(包括问号)
使用[^\?]+
我得到了正确的部分,直到第一个问号,使用
[^\s]+
我得到了正确的部分,直到第一个空格
因为这些字符类的意思是:所有没有问号的字符,或者:所有没有空格的字符
要组合它们,您需要说:所有既不是问号也不是空格的字符:
[^?\s]+
对于您使用过的OR,它只是尝试了第一个(
[^\?]+
-包括空格),成功了,并且会回溯并尝试[^\s]+
(包括问号)如果第一个不起作用,下面的正则表达式将只匹配紧跟在toGET
或POST
之后的字符串,后跟空格或?
符号
(?<=GET |POST )\s*.*?(?= |\?)
说明:
(?<= 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
(?下面的正则表达式将只匹配紧跟在toGET
或POST
之后的字符串,后跟空格或?
符号
(?<=GET |POST )\s*.*?(?= |\?)
说明:
(?<= 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
(?