域名正则表达式 [英] Domain name regex
本文介绍了域名正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图从 URL 中提取域名.例如:
Trying to extract the domain name out of URL. For example:
x <-"https://stackoverflow.com/questions/ask"
到:stackoverflow.com
我从这个问题中找到了以下正则表达式.正则匹配主域名.
I found the following regex from this question. regex match main domain name.
regex <- "([0-9A-Za-z]{2,}\\[0-9A-Za-z]{2,3}\\[0-9A-Za-z]{2,3}|[0-9A-Za-z]{2,}\\[0-9A-Za-z]{2,3})$"
但是当我尝试使用 stringr
包中的 str_extract
时,R 似乎不理解它.
But R doesn't seem to understand it when I try to use str_extract
from the stringr
package.
x2 <- str_extract(x, regex)
推荐答案
为什么不使用 XML
中的 parseURI
?它将 URL 分解为不同的元素.
Why not use parseURI
from XML
? It breaks a URL into its different elements.
x <- "http://stackoverflow.com/questions/ask"
library(XML)
parseURI(x)$server
# [1] "stackoverflow.com"
这篇关于域名正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文