Regex URL Match

后端 未结 3 1304
滥情空心
滥情空心 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: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.

提交回复
热议问题