正则表达式 URL 匹配 [英] Regex URL Match
问题描述
我才真正开始学习如何使用正则表达式,我正在尝试创建一个来匹配网址.到目前为止,我有:
I just really started learning how to use regex's and i am trying to create one to match urls. So far i have:
(http://|https://|www|\w)+\.[\w]{2,4}[^\s]+
谁能给我一些关于它看起来如何的反馈或建议,或者给我指出更好的
can anyone give me some feedback or advice on how this looks, or maybe point me in a better
推荐答案
你写的正则表达式是一个好的开始,正则表达式的真正力量在于它可以非常......好吧......强大:)URL 的结构有很多差异,仍然被认为是可以的.
the regex you wrote is a good start, the true power of regex is that it can be very... well... powerful :) there are a lot of differences in the structure of URL that are still considered ok.
查阅正则表达式参考以了解更多信息并理解所有字符的含义.
consult a Regex Reference to learn more and understand the meaning of all the characters.
并且您可以通过访问像这个
这对于您需要的东西来说可能过于复杂,但这是一个写得很好的 URLmatching 正则表达式:^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$
this may be overly complex for what you need but this is a well written regex for URLmatching: ^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$
顺便说一句,我使用这个在线 Regex Helper 来测试我的正则表达式字符串,而无需实际从代码运行它们.
on a side note i use this online Regex Helper to test my regex strings without the need to actually run them from the code.
这篇关于正则表达式 URL 匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!