Regex to match URL

后端 未结 14 2330
耶瑟儿~
耶瑟儿~ 2020-11-22 09:41

I am using the following regex to match a URL:

$search  = \"/([\\S]+\\.(MUSEUM|TRAVEL|AERO|ARPA|ASIA|COOP|INFO|NAME|BIZ|CAT|COM|INT|JOBS|NET|ORG|PRO|TEL|AC|A         


        
14条回答
  •  执笔经年
    2020-11-22 09:49

    $search  = "#^((?#
        the scheme:
      )(?:https?://)(?#
        second level domains and beyond:
      )(?:[\S]+\.)+((?#
        top level domains:
      )MUSEUM|TRAVEL|AERO|ARPA|ASIA|EDU|GOV|MIL|MOBI|(?#
      )COOP|INFO|NAME|BIZ|CAT|COM|INT|JOBS|NET|ORG|PRO|TEL|(?#
      )A[CDEFGILMNOQRSTUWXZ]|B[ABDEFGHIJLMNORSTVWYZ]|(?#
      )C[ACDFGHIKLMNORUVXYZ]|D[EJKMOZ]|(?#
      )E[CEGHRSTU]|F[IJKMOR]|G[ABDEFGHILMNPQRSTUWY]|(?#
      )H[KMNRTU]|I[DELMNOQRST]|J[EMOP]|(?#
      )K[EGHIMNPRWYZ]|L[ABCIKRSTUVY]|M[ACDEFGHKLMNOPQRSTUVWXYZ]|(?#
      )N[ACEFGILOPRUZ]|OM|P[AEFGHKLMNRSTWY]|QA|R[EOSUW]|(?#
      )S[ABCDEGHIJKLMNORTUVYZ]|T[CDFGHJKLMNOPRTVWZ]|(?#
      )U[AGKMSYZ]|V[ACEGINU]|W[FS]|Y[ETU]|Z[AMW])(?#
        the path, can be there or not:
      )(/[a-z0-9\._/~%\-\+&\#\?!=\(\)@]*)?)$#i";
    

    Just cleaned up a bit. This will match only HTTP(s) addresses, and, as long as you copied all top level domains correctly from IANA, only those standardized (it will not match http://localhost) and with the http:// declared.

    Finally you should end with the path part, that will always start with a /, if it is there.

    However, I'd suggest to follow Cerebrus: If you're not sure about this, learn regexps in a more gentle way and use proven patterns for complicated tasks.

    Cheers,

    By the way: Your regexp will also match something.r and something.h (between |TO| and |TR| in your example). I left them out in my version, as I guess it was a typo.

    On re-reading the question: Change

      )(?:https?://)(?#
    

    to

      )(?:https?://)?(?#
    

    (there is a ? extra) to match 'URLs' without the scheme.

提交回复
热议问题