如何通过正则表达式匹配从多个字符(可以嵌套)开始的字符串?

时间:2019-01-15 19:25:48

标签: regex

我正在尝试使正则表达式从不同类型的url中获取域。

我正在使用正则表达式,它可以与域部分中不带@的链接一起正常工作,例如:

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

/(?<=(\/\/))[^\n|\/|:]+/g 对于带有@的链接(例如http://regex@regex.com),可以将\/\/替换为\@/(?<=(@))[^\n|\/|:]+/g

但是当我试图使正则表达式匹配这两种情况并使得 /(?<=((\/\/)|(\@)))[^\n|\/|:]+/g 它没用。

1 个答案:

答案 0 :(得分:0)

如果字符串中出现字符串://,(正向查找),则表示该字符串为域,您需要在此之后捕获所有内容。是否具有@

案例1
://

之后捕获整个字符串

正则表达式:
(?<=\:\/\/).*

说明:

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)

示例 https://regex101.com/r/jsqqw8/1/

案例2
仅捕获://

之后的域

正则表达式:
(?<=:\/\/)[^\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:
如果文本中没有://,则捕获@之后的域;如果文本中没有@,则捕获文本。

正则表达式:
(?!:\/\/)(?:[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