使用正则表达式提取字符串?

时间:2018-10-10 10:10:30

标签: regex

Sample Data:

+---------------------------------------------------------------------------------+
|refererurl                                                                       |
+---------------------------------------------------------------------------------+
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|http://mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com/                        |
|http://mbappgewtgobzgu4dcmrtgy888888.com/                                        |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|http://mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com/                        |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|null                        |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|http://mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com/                        |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|http://mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com/                        |
|https://www.tesco.com/direct/party-gifts-flowers/helium-canisters/cat31450037.cat|
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
|https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html                          |
+---------------------------------------------------------------------------------+

我想要正则表达式如下:

a。我想要一个正则表达式表达式,它可以开始在“ .com”之前向后提取,直到包含.com的网站名称
例如
https://www.tesco.com/groceries/dfp/dfp-beaa1a3b14.html-> tesco.com http://mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com-> mbappgiwwg33nfz2gk43dn4xgo4tpmnsxe6joozuwk5y8.com

2 个答案:

答案 0 :(得分:0)

以下正则表达式似乎可以在这里工作:

[^.\/]+.com

Demo

请注意,这不会考虑诸如tesco.co.uk.com之类的网址,在这种情况下,我们需要做更多的工作。

答案 1 :(得分:0)

尝试这个:

(?:http(?:s)?:\/\/(?:www.)?)(.*?)\/

它甚至可以与url一起使用: www.example.co.uk/qsdqsd.html

DEMO