Как сопоставить строку, начинающуюся с нескольких символов (может быть вложенной) через регулярное выражение? - PullRequest
0 голосов
/ 15 января 2019

Я пытаюсь сделать регулярное выражение, чтобы получить домен из разных типов URL.

Я использую регулярное выражение, которое работает нормально либо со ссылками без @ в доменной части, например:

https://stackoverflow.com/questions/ask
https://regexr.com/

/(?<=(\/\/))[^\n|\/|:]+/g Для ссылок с @ (например, http://regex@regex.com) работает с заменой \/\/ на \@: /(?<=(@))[^\n|\/|:]+/g

Но когда я пытаюсь сделать регулярное выражение для сопоставления обоих этих случаев и сделать /(?<=((\/\/)|(\@)))[^\n|\/|:]+/g это не работает.

1 Ответ

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

Вы должны искать строку ://, (положительный взгляд сзади), если она входит в строку, значит, это домен, и вам нужно захватить все после этого. Имеет ли он @ или нет.

Дело 1
Захват всей строки после ://

Regex:
(?<=\:\/\/).*

Объяснение:

Positive Lookbehind (?<=\:\/\/) Assert that the Regex below matches
\: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

* +1034 * Пример * 1 036 * https://regex101.com/r/jsqqw8/1/ Дело 2
Захват только домена после :// Regex:
(?<=:\/\/)[^\n|\/|:]+ Пояснение:
Positive Lookbehind (?<=:\/\/)
Assert that the Regex below matches
: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
Match a single character not present in the list below [^\n|\/|:]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
| matches the character | literally (case sensitive)
\/ matches the character / literally (case sensitive)
|: matches a single character in the list |: (case sensitive) Дело 3:
Захват домена после ://, если в тексте нет @ и если в тексте присутствует @, захватить текст после этого. Regex:
(?!:\/\/)(?:[A-z]+\.)*[A-z][A-z]+\.[A-z]{2,} Объяснение: Negative Lookahead (?!:\/\/) Assert that the Regex below does not match : matches the character : literally (case sensitive) \/ matches the character / literally (case sensitive) \/ matches the character / literally (case sensitive) Non-capturing group (?:[A-z]+\.)* * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) Match a single character present in the list below [A-z]+ + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) A-z a single character in the range between A (index 65) and z (index 122) (case sensitive) \. matches the character . literally (case sensitive) Match a single character present in the list below [A-z] A-z a single character in the range between A (index 65) and z (index 122) (case sensitive) Match a single character present in the list below [A-z]+ + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) A-z a single character in the range between A (index 65) and z (index 122) (case sensitive) \. matches the character . literally (case sensitive) Match a single character present in the list below [A-z]{2,} {2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy) A-z a single character in the range between A (index 65) and z (index 122) (case sensitive) Пример:
https://regex101.com/r/jsqqw8/4
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...