我正在寻找一种方法来提取“*”和“+”符号在 R 字符串中的位置。
test <- "x+y"
unlist(gregexpr("+", test))
[1] 1 2 3
unlist(gregexpr("y", test))
[1] 3
它返回 x 或 y 的位置,但返回 + 或 * 的所有位置。
谢谢!
最佳答案
使用 fixed = TRUE
,默认为 FALSE
并使用 +
是元字符的正则表达式模式。根据 ?regex
+
- The preceding item will be matched one or more times.
*
- The preceding item will be matched zero or more times.
unlist(gregexpr("+", test, fixed = TRUE))
[1] 2
关于r - 使用R提取字符串中 "+"和 "*"符号的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70500143/