r - 如何覆盖R中的html文件
问题描述
我正在尝试将 html 文件中的电子邮件地址替换为 ANTI SPAM 格式,然后再次将其导出为 nospam.html 文件。我尝试使用 gsub() 函数来执行此操作,但它似乎不起作用。有什么问题?谢谢!!!
datei <- scan("https://isor.univie.ac.at/about-us/People.html", sep = "\n", what= "character")
#pattern.email <- "[a-z]+[.]+[a-z]+?[@]+[a-z]+"
reg.email <- "\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>" #works
stelle.email <-gregexpr(reg.email, datei, ignore.case = TRUE) #works
unlist(stelle.email)
res.email<- regmatches(datei, stelle.email)
datei2<-gsub(reg.email, "vornameDOTnameNO-SPAMunivieDOTacDOTat", x = datei)
write(datei2, file = "nospam.html")
解决方案
知道regmatches
(用于提取匹配的子字符串)还具有伴随regmatches<-
功能(用于替换匹配的子字符串)可能会提供有用的信息。见?regmatches
。
所以不需要gsub
,只需:
datei <- scan("https://isor.univie.ac.at/about-us/People.html", sep = "\n", what= "character")
# Read 481 items
reg.email <- "\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>" #works
stelle.email <- gregexpr(reg.email, datei, ignore.case = TRUE) #works
# for proof, first look at a substring with a "known" email:
substr(datei[268], 236, 281)
### the only new/different line of code, remove your gsub
regmatches(datei, stelle.email) <- "vornameDOTnameNO-SPAMunivieDOTacDOTat"
# now look at the same portion of that one substring, now updated
substr(datei[268], 236, 281)
write(...)
推荐阅读
- javascript - 获取实例的类名并使用它在 JS 中创建新实例
- keyboard-shortcuts - 工作流程在 Automator 中不起作用:执行自定义键盘快捷键的问题
- react-native - 直接重定向到linkedin,不用先去auth0登录页面
- c++ - 向加权无向图添加顶点时,保留哪个权重?
- oracle-sqldeveloper - 失去了SQL开发者的所有“记忆”,还能找回来吗?
- ruby-on-rails - Turbo_stream 格式不再发送
- java - studio3T 中的 MongoDB 查询。获取特定品牌的平均价格
- api - 将binance api数据导入谷歌表格
- typescript - 打字稿中的 d3 层次结构会为 .sum 上的值抛出错误?
- microsoft-graph-api - 邮件属性未保存在 UpdateAsync