r - 如何用 stringr 中的 str_replace 替换插入符号
问题描述
我有一个代码问题需要一些帮助(排除特定字符串)。
我找到了str_replace_all
做这项工作。但是,它适用于其他
“/n”、“/t”或“A”、“B”、“C”等字符,除了“^”,我想排除
此标志但收到错误消息
(stri_replace_all_regex 中的错误(字符串,模式,fix_replacement(替换),:括号表达式上缺少右括号。(U_REGEX_MISSING_CLOSE_BRACKET))
谢谢你的帮助!
code=c("^GSPC","^FTSE","000001.SS","^HSI","^FCHI","^KS11","^TWII","^GDAXI","^STI")
str_replace_all(code, "([^])", "")
解决方案
一个选项是包装fixed
并且应该没问题
library(stringr)
str_replace_all(code, fixed("^"), "")
#[1] "GSPC" "FTSE" "000001.SS" "HSI" "FCHI" "KS11" "TWII" "GDAXI" "STI"
此外,当我们用空白 ( ""
) 替换时,一个选项是str_remove
str_remove(code, fixed("^"))
Regarding why the OP's code didn't, inside the square brackets, if we use ^
, it is not reading the literal character, instead the metacharacter in it looks for characters other than and here it is blank ([^]
)
推荐阅读
- c - Having trouble concatenating first name and last name
- maven - 为什么 maven deploy 会生成额外的 jars
- amazon-web-services - Dockerfile - 将配置文件复制到适当的容器
- javascript - 调用数据键值的方法?
- bash - 在 bash 脚本中,如何遍历按日期排序的文件
- angular - NgxNotification 组件在生产构建中失败。角 7
- mips - MIPS - 如何在不使用转换指令的情况下将浮点输入转换为整数,并且只使用移位或旋转?
- html - CSS Submeu 出现在悬停时,但当我尝试转到子菜单时消失
- python-3.x - Bin 随机值使用 .cut() on 并将 bins 放入新的 Pandas 数据框列
- php - 循环 2 个数组并比较是否存在相等的值(未定义的偏移量)