Regex URL Match

后端 未结 3 1303
滥情空心
滥情空心 2021-01-29 07:43

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]+


        
相关标签:
3条回答
  • 2021-01-29 08:03

    Give this one a try: ^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$ Note that depending on how you are using it, you might want to remove the ^ and $ from the beginning and end.

    0 讨论(0)
  • 2021-01-29 08:23

    Plenty of stuff on the web...

    • You can look at what we do within Horde: Horde URL filter
    • Ours has been derived from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
    • And there is also PHP validation/regex for URL
    0 讨论(0)
  • 2021-01-29 08:26

    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.

    and you can check for what the community generates by going to a site like this one

    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\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$

    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.

    0 讨论(0)
提交回复
热议问题