删除R中除反斜杠以外的所有标点符号

删除R中除反斜杠以外的所有标点符号,r,regex,strsplit,R,Regex,Strsplit,我试图从数据集中提取html链接。我正在使用strsplit和grep来查找带有链接的子字符串,但是结果在字符串的开头或结尾都有不需要的字符……我如何才能仅提取具有所需模式的字符串或保留具有所需模式的字符串 他就是我现在正在做的 1) 我使用strplit和“”(空格)作为分隔符拆分了一段文本 2) 接下来,我将strsplit的结果grep以查找模式 e、 g.grep(“https:\/\/support.google.com\/blogger\/topic\/[0-9]”,r) 3) 结果

我试图从数据集中提取html链接。我正在使用strsplit和grep来查找带有链接的子字符串,但是结果在字符串的开头或结尾都有不需要的字符……我如何才能仅提取具有所需模式的字符串或保留具有所需模式的字符串

他就是我现在正在做的

1) 我使用strplit和“”(空格)作为分隔符拆分了一段文本

2) 接下来,我将strsplit的结果grep以查找模式

e、 g.grep(“https:\/\/support.google.com\/blogger\/topic\/[0-9]”,r)

3) 结果的几个变化如下所示

https://support.google.com/blogger/topic/12457 
https://support.google.com/blogger/topic/12457.
[https://support.google.com/blogger/topic/12457]  
<<https://support.google.com/blogger/topic/12457>>
https://support.google.com/blogger/topic/12457,
https://support.google.com/blogger/topic/12457),
xxxxxxhttps://support.google.com/blogger/topic/12457),hhhththta
etc...
https://support.google.com/blogger/topic/12457 
https://support.google.com/blogger/topic/12457.
[https://support.google.com/blogger/topic/12457]  
https://support.google.com/blogger/topic/12457,
https://support.google.com/blogger/topic/12457),
xxxxxxhttps://support.google.com/blogger/topic/12457),hhthta
等
如何仅提取“”或在提取脏数据后如何删除不需要的标点符号


Thx提前。

如果数据在某个时候是HTML,您可以尝试以下方法:

library(XML)
urls <- getNodeSet(htmlParse(htmldata), "//a[contains(@href, 'support.google.com')]/@href"))
库(XML)

urlqdapRegex
包有一个很棒的函数,名为
rm_url
,非常适合这个例子

install.packages('qdapRegex')
library(qdapRegex)

urls <- YOUR_VECTOR_OF_URLS
rm_url(urls, extract = T)
install.packages('qdapRegex'))
图书馆(qdapRegex)
URL使用可能会使这类任务变得简单一些

# generate dataset
x <- c(
"https://support.google.com/blogger/topic/12457
https://support.google.com/blogger/topic/12457.
https://support.google.com/blogger/topic/12457] 
<<https://support.google.com/blogger/topic/12457>>
https://support.google.com/blogger/topic/12457,
https://support.google.com/blogger/topic/12457),
xxxxxxhttps://support.google.com/blogger/topic/12457),hhhththta")

# extract urls
# note you don't have to worry about escaping the html string yourself
library(rex)    
re <- rex(
  capture(name = "url",
    "https://support.google.com/blogger/topic/",
    digits
    ))

re_matches(x, re, global = TRUE)[[1]]
#>                                             url
#>1 https://support.google.com/blogger/topic/12457
#>2 https://support.google.com/blogger/topic/12457
#>3 https://support.google.com/blogger/topic/12457
#>4 https://support.google.com/blogger/topic/12457
#>5 https://support.google.com/blogger/topic/12457
#>6 https://support.google.com/blogger/topic/12457
#>7 https://support.google.com/blogger/topic/12457
#生成数据集
如果它们都以数字结尾,请尝试
gsub(“*(http.*\\d)。*”,“\\1”,x)