开发者

Matching patterns in URL with a language similar to regexp, but specific for URLs

I use matching URLs in a part of my code. Now I use regular expressions for this. This is fine, but does not always produces "nice", simply readable patterns. Is there any language defined for matching URLs? It should be like this: http://*.example.com/* so simply wild-cards and things useful for URL would be there.

The best would if these expression can be simply transformed to regexp. Do you know specification for such a language, or even an implementation, preferably for ruby.. otherwise I implement it my开发者_如何学运维self... the key is the readability of the patterns. Thanks for help!


You'd have to carefully work your syntax out before you begin. At first glance, what you intend would be easily achieved by translating your syntax into ordinary regexes:

s = 'http://*.example.com/*' #=> "http://*.example.com/*"
r = Regexp.compile("^#{Regexp.escape(s).gsub('\*','.*')}$") #=> http:examplecom
'http://test.example.com/path/to/doc.html' =~ r #=> 0
'http://test.example2.com/path/to/doc.html' =~ r #=> nil


URLs can be a bit tricky to parse correctly, especially if you want to be standards compliant. This is why Ruby has the uri builtin library.

For a more advanced parsing library with placeholders like you want, you should look into the addressable gem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜