【发布时间】:2021-10-24 22:23:56
【问题描述】:
我想在向量上找到任何元素,其中包含 ',' 后面没有空格。
我在不同的来源找到了:
- \b 或 \s 代表空格
- \S 是除了空白之外的一切
- [^] 应该是除了 之外的所有内容
为什么这些不会产生预期的结果?考虑到 /S 是除了空格之外的所有内容,而 ^\s 应该意味着相同...
grepl(',[^\b]', c('a,', 'b, ', 'c,')) # output: FALSE TRUE FALSE expected: TRUE FALSE TRUE
gsub(',[^\b]', 'here', c('a,', 'b, ', 'c,')) # output: "a," "bhere" "c," expected: "ahere" "b" "chere"
gsub(',[^\\s]', 'here', c('a,', 'b, ', 'c,')) # output: "a," "bhere" "c," expected: "ahere" "b" "chere"
gsub(',[^\\S]', 'here', c('a,', 'b, ', 'c,')) # output: "a," "bhere" "c," expected: "a," "bhere" "c,"
gsub(',(\\S)', 'here', c('a,', 'b, ', 'c,')) # output: "a," "b, " "c," expected: "ahere" "b" "chere"
这不是家庭作业,这是一个最小的工作示例。
【问题讨论】:
-
你的预期输出是什么?
-
编辑了代码部分以包含预期的输出。