简体   繁体   English

/的正则表达式

[英]Regular expression for /

I am trying a regular expression through the Python re module to match both of these patterns: 我正在尝试通过Python re模块来匹配两个模式的正则表达式:

"GET /images/launch-logo.gif HTTP/1.0"
"GET / HTTP/1.0 "

I tried the following expression: 我尝试了以下表达式:

"(\S+) (\S.*?)\s*(\S*)"

This does as expected by returning the following: 通过返回以下内容,可以达到预期的效果:

1. GET
2. /images/launch-logo.gif
3. HTTP/1.0

However, for the second one it returns: 但是,对于第二个它返回:

1. GET
2. / HTTP/1.0
3. ''

Instead, I would like that to return the following: 相反,我希望返回以下内容:

1. GET
2. / 
3. HTTP/1.0

There is also a trailing space that needs to be removed. 还需要删除尾随空间。 Could some one help me with the right regular expression? 有人可以帮我正确的正则表达式吗?

You don't need to use a reluctant quantifier ( *? ) here. 您无需在此处使用勉强的量词( *? )。 Use: 采用:

(\S+)\s+(\S+)\s+(\S+)\s*

The problem with your original regex is the combination of .*? 您原始正则表达式的问题是.*?的组合.*? and \\s* , since the reluctant expression can keep matching while \\s* doesn't have to match anything. \\s* ,因为勉强的表达式可以保持匹配,而\\s*不必匹配任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM