如何为这个表达式写一个正则表达式?

时间:2010-09-14 07:28:40

标签: python regex

url1 = http://xyz.com/abc
url2 = http://xyz.com//abc

我想写一个regex来验证url1url2

4 个答案:

答案 0 :(得分:4)

为什么不直接使用urlparse

答案 1 :(得分:0)

http://\w+\.\w+//?\w+

答案 2 :(得分:0)

答案取决于您是否要解析一般的网址,或者您是否只是想知道如何处理可选的斜杠。

在第一种情况下,我同意Amber你应该使用urlparse。

在第二种情况下,在表达式中的斜杠后使用?

http://xyz.com//?abc

正则表达式中的?表示前一个元素是可选的(即可能出现零次或一次)。

答案 3 :(得分:0)

您可以使用此正则表达式:

\w{4}\:\/{2}\w+\.\w+\/{1,2}\w+

说明:

\w{4} match any word character [a-zA-Z0-9_]
    Quantifier: Exactly 4 times
\: matches the character : literally
\/{2} matches the character / literally
    Quantifier: Exactly 2 times
\w+ match any word character [a-zA-Z0-9_]
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed
\. matches the character . literally
\w+ match any word character [a-zA-Z0-9_]
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed
\/{1,2} matches the character / literally
    Quantifier: Between 1 and 2 times, as many times as possible, giving back as needed 
\w+ match any word character [a-zA-Z0-9_]
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed

希望这会有所帮助。