Regex для определения имени пользователя на нескольких сайтах - PullRequest
0 голосов
/ 08 января 2019

Я пытаюсь создать регулярное выражение для идентификации имени пользователя на нескольких сайтах.

Существует несколько сайтов, которые могут быть доменными именами, и я могу добавить их в будущем.

Оттуда я ищу @xxxxxxx непосредственно после домена / или после домена / тега / @ xxxxxxxx. После имени пользователя может быть неизвестное количество символов, а иногда может быть / и неизвестный контент после этого, меня это не касается.

Я в основном просматриваю список доменов с / без http / https, затем смотрю на первую или вторую позицию для @alphanumeric до следующего / или пустого.

Пример URL:

https://site1.com/@bob
https://site2.com/boats/@frank/how-to-fix-your-boat
http://site2.com/@frank/settings
site1.com/@joe.beans/re-how-to-fix-your-boat-248435252

Я пытаюсь определить @username по каждому типу URL, который может отображаться.

Я буду вести список доменов, в которых ведется поиск, некоторые из них могут быть добавлены в будущем. Я буду использовать JS для итерации списка и заполнения этой части регулярного выражения.

Я считаю, что регулярное выражение будет самым быстрым способом сделать это, если только не будет доступно что-то еще, расширение chrome, которое будет проще.

1 Ответ

0 голосов
/ 08 января 2019

Вы можете использовать регулярное выражение /(.+)\/@([^\/\r\n]+)/ для захвата как сайта, так и имени пользователя следующим образом:

var re = /(.+)\/@([^\/\r\n]+)/;
var match = re.exec(url);
if (match != null) {
    site = match[1];
    user = match[2];
}

Используя ваши примеры:

'https://site1.com/@bob'                                  --> site = "https://site1.com";       user = "bob"
'https://site2.com/boats/@frank/how-to-fix-your-boat'     --> site = "https://site2.com/boats"; user = "frank"
'http://site2.com/@frank/settings'                        --> site = "http://site2.com";        user = "frank"
'site1.com/@joe.beans/re-how-to-fix-your-boat-248435252'  --> site = "site1.com";               user = "joe.beans"


EDIT

Если вы хотите захватить протокол, домен и пользователя, то это должно сделать это:

var re = /^((?:http|ftp)s?:\/\/)?(?:www\.)?([^@\/\r\n]+)?(?:\/.+)?\/@([^\/\r\n]+)/;
var match = re.exec(url);
if (match != null) {
    protocol = match[1];
    domain   = match[2];
    user     = match[3];
}

Это даст:

url                                                         match[1]  match[2]   match[3]
---                                                         --------  --------   --------
https://site1.com/@bob                                  --> https://  site1.com  bob
https://site2.com/boats/@frank/how-to-fix-your-boat     --> https://  site2.com  frank
http://site2.com/@frank/settings                        --> http://   site2.com  frank
site1.com/@joe.beans/re-how-to-fix-your-boat-248435252  -->           site1.com  joe.beans

Regex Details

"^" +                Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator)
"(" +                Match the regex below and capture its match into backreference number 1
   "(?:" +           Match the regular expression below
                     Match this alternative (attempting the next alternative only if this one fails)
         "http" +    Match the character string “http” literally (case insensitive)
      "|" +
                     Or match this alternative (the entire group fails if this one fails to match)
         "ftp" +     Match the character string “ftp” literally (case insensitive)
   ")" +
   "s" +             Match the character “s” literally (case insensitive)
      "?" +          Between zero and one times, as many times as possible, giving back as needed (greedy)
   ":" +             Match the character “:” literally
   "\\/" +           Match the character “/” literally
   "\\/" +           Match the character “/” literally
")" +
   "?" +             Between zero and one times, as many times as possible, giving back as needed (greedy)
"(?:" +              Match the regular expression below
   "www" +           Match the character string “www” literally (case insensitive)
   "\\." +           Match the character “.” literally
")" +
   "?" +             Between zero and one times, as many times as possible, giving back as needed (greedy)
"(" +                Match the regex below and capture its match into backreference number 2
   "[^" +            Match any single character NOT present in the list below
      "@" +          The literal character “@”
      "\\/" +        The literal character “/”
      "\r" +         The carriage return character
      "\n" +         The line feed character
   "]" +
      "+" +          Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
   "?" +             Between zero and one times, as many times as possible, giving back as needed (greedy)
"(?:" +              Match the regular expression below
   "/" +             Match the character “/” literally
   "." +             Match any single character that is NOT a line break character (line feed, carriage return, line separator, paragraph separator)
      "+" +          Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
   "?" +             Between zero and one times, as many times as possible, giving back as needed (greedy)
"/@" +               Match the character string “/@” literally
"(" +                Match the regex below and capture its match into backreference number 3
   "[^" +            Match any single character NOT present in the list below
      "\\/" +        The literal character “/”
      "\r" +         The carriage return character
      "\n" +         The line feed character
   "]" +
      "+" +          Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...