【发布时间】:2014-02-24 17:53:27
【问题描述】:
我正在尝试编写一个从 URL 捕获域和路径的正则表达式。我试过了:
https?:\/\/(.+)(\/.*)
Match 1
0. google.com
1. /foo
但不是我对http://example.com/foo/bar 的期望:
预期:
Match 1
0. google.com
1. /foo/bar
实际:
Match 1
0. google.com/foo
1. /bar
我做错了什么?
【问题讨论】:
-
你有什么理由想用正则表达式来做这件事吗?标准库中的
urlparse模块可以做到这一点以及更多。 -
@DanielRoseman urlparse 在分解 URL 方面做得很好,但我想要包含查询、参数和片段的路径。这对其他情况很有用。谢谢!