За последние 24 часа нас посетили 49937 программистов и 1400 роботов. Сейчас ищет 1191 программист ...

Вытащить имя домена из ссылки

Тема в разделе "Регулярные выражения", создана пользователем Юрий Юрков, 24 окт 2007.

  1. Юрий Юрков

    Юрий Юрков Активный пользователь

    С нами с:
    25 окт 2006
    Сообщения:
    16
    Симпатии:
    0
    Есть ссылки различного формата:
    www.example.com
    http://example.com/?bla/bla.php
    example.com/bla/bla
    ... и так далее

    Подскажите как получить только домен example.com
     
  2. Sergey89

    Sergey89 Активный пользователь

    С нами с:
    4 янв 2007
    Сообщения:
    4.796
    Симпатии:
    0
  3. virabhadra

    virabhadra Активный пользователь

    С нами с:
    11 дек 2006
    Сообщения:
    127
    Симпатии:
    0
    Адрес:
    Praha, Czech Republic
    Ссылка на стандарт: http://www.ietf.org/rfc/rfc2396.txt

    Код (Text):
    1. B. Parsing a URI Reference with a Regular Expression
    2.  
    3.    As described in Section 4.3, the generic URI syntax is not sufficient
    4.    to disambiguate the components of some forms of URI.  Since the
    5.    "greedy algorithm" described in that section is identical to the
    6.    disambiguation method used by POSIX regular expressions, it is
    7.    natural and commonplace to use a regular expression for parsing the
    8.    potential four components and fragment identifier of a URI reference.
    9.  
    10.    The following line is the regular expression for breaking-down a URI
    11.    reference into its components.
    12.  
    13.       ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
    14.        12            3  4          5       6  7        8 9
    15.  
    16.    The numbers in the second line above are only to assist readability;
    17.    they indicate the reference points for each subexpression (i.e., each
    18.    paired parenthesis).  We refer to the value matched for subexpression
    19.    <n> as $<n>.  For example, matching the above expression to
    20.  
    21.       http://www.ics.uci.edu/pub/ietf/uri/#Related
    22.  
    23.    results in the following subexpression matches:
    24.  
    25.       $1 = http:
    26.       $2 = http
    27.       $3 = //www.ics.uci.edu
    28.       $4 = www.ics.uci.edu
    29.       $5 = /pub/ietf/uri/
    30.       $6 = <undefined>
    31.       $7 = <undefined>
    32.       $8 = #Related
    33.       $9 = Related