Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r删除以破折号开头和结尾的单词_R_Regex - Fatal编程技术网

r删除以破折号开头和结尾的单词

r删除以破折号开头和结尾的单词,r,regex,R,Regex,如何从此类字符串中删除带破折号(作为前缀或后缀)的单词: x我建议使用 x <- "word -o -dod -3 -33 dp-pd -d- --- 140 -- s- S- SS- s3- 3e- 33- 3- s SS avf-ada -" trimws(gsub("(?:\\S+-\\B|\\B-\\S+|\\B-\\B)\\s*", "", x, perl=TRUE)) x无需担心。多亏了regex101,我找到了一个解决方案(\s-\s+)(\s+-\s)那-o word-o

如何从此类字符串中删除带破折号(作为前缀或后缀)的单词:

x我建议使用

x <- "word -o -dod -3 -33 dp-pd -d- --- 140 -- s- S- SS- s3- 3e- 33- 3- s SS avf-ada -"
trimws(gsub("(?:\\S+-\\B|\\B-\\S+|\\B-\\B)\\s*", "", x, perl=TRUE))

x无需担心。多亏了regex101,我找到了一个解决方案
(\s-\s+)(\s+-\s)
-o word-o
呢?我明白了。我的代码与第一个令牌不匹配。这并不是一个很大的损失,但无论如何,谢谢你这么开明。我发布了一个解决方案。见下文。谢谢。然而,ideone.com工作得很好,但在regex101上它不匹配单破折号。但我不是专家。不知道你说的单破折号是什么意思,你的正则表达式也不匹配单破折号。好的。对不起,有点乱。我已经在模式中添加了一个片段:
(?:\S+-\B |\B-\S+\B-\B)
,所以这就是我现在想要的。我知道了,我更新了答案并添加了解释。