【发布时间】:2020-11-17 20:20:51
【问题描述】:
这个我已经看过了,但不是我需要的:
情况:使用gsub,我想清理字符串。这些是我的条件:
- 只保留文字(没有数字或“奇怪”符号)
- 将这些单词与(仅一个)
' - _ $ .之一分开。例如:don't,re-loading,come_home,something$col - 保留具体名称,例如
package::function或package::function()
所以,我有以下内容:
[^A-Za-z]([a-z]+)(-|'|_|$)([a-z]+)([a-z]+(_*)[a-z]+)(::)([a-z]+(_*)[a-z]+)(\(\))*
示例:
如果我有以下情况:
# Re-loading pkgdown while it's running causes weird behaviour with # the context cache don't
# Needs to handle NA for desc::desc_get()
# Update href of toc anchors , use "-" instead "."
# Keep something$col or here_you::must_stay
我想要
Re-loading pkgdown while it's running causes weird behaviour with the context cache don't
Needs to handle NA for desc::desc_get()
Update href of toc anchors use instead
Keep something$col or here_you::must_stay
问题:我有几个:
A.第二个表达式无法正常工作。目前,它仅适用于 - 或 '
B.如何在 R 中将所有这些组合到一个 gsub 中?我想做gsub(myPatterns, myText) 之类的事情,但不知道如何修复和组合所有这些。
【问题讨论】:
-
试试
trimws(gsub("(?:\\w+::\\w+(?:\\(\\))?|\\p{L}+(?:[-'_$]\\p{L}+)*)(*SKIP)(*F)|[^\\p{L}\\s]", "", myText, perl=TRUE))。见the regex demo。 -
这就像一个魅力!你能把它作为答案吗?