开发者

how to write a regex for this expression?

开发者 https://www.devze.com 2023-01-16 05:29 出处:网络
url1 = http://xyz.com/abc url2 = http://xyz.com//abc I want to write a re开发者_开发技巧gex that validate both url1 and url2Why not just use urlparse instead?http://\\w+\\.\\w+//?\\w+
url1 = http://xyz.com/abc
url2 = http://xyz.com//abc

I want to write a re开发者_开发技巧gex that validate both url1 and url2


Why not just use urlparse instead?


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


The answer depends on whether you want to parse urls in general or whether you just wonder how to handle the optional slash.

In the first case, I agree with Amber that you should use urlparse.

In the second case, use a ? after the slash in your expression:

http://xyz.com//?abc

A ? in a regular expression means that the previous element is optional (i.e. may appear zero times or once).


You can use this regex:

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

explanation:

\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

hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消