R 删除单词和数字之间的空格
灵感来自 我们如何将一个单词和一个由空格分隔的数字组合成一个字符串 比如说R 删除单词和数字之间的空格,r,regex,R,Regex,灵感来自 我们如何将一个单词和一个由空格分隔的数字组合成一个字符串 比如说 There is a cooling problem in the component tx 313 leakage found in irc 256, fixed by replacement Roasted cable in cpt trx235 结果应该是 There is a cooling problem in the component tx313 leakage found in irc256, fixe
There is a cooling problem in the component tx 313
leakage found in irc 256, fixed by replacement
Roasted cable in cpt trx235
结果应该是
There is a cooling problem in the component tx313
leakage found in irc256, fixed by replacement
Roasted cable in cat trx235
我对regex不熟悉,所以想不出一个方法来做
感谢您的帮助text=c(“组件tx 313中存在冷却问题”,“irc 256中发现泄漏,通过更换修复”,
text=c("There is a cooling problem in the component tx 313","leakage found in irc 256, fixed by replacement",
"Roasted cable in cpt trx235","word 123 456")
gsub("(?<=[a-z]) (?=\\d)","",text,perl = T)
[1] "There is a cooling problem in the component tx313" "leakage found in irc256, fixed by replacement"
[3] "Roasted cable in cpt trx235" "word123 456"
“cpt trx235中的烘烤电缆”,“字123 456”)
gsub((?text=c)(“部件tx 313中存在冷却问题”,“irc 256中发现泄漏,通过更换修复”,
“cpt trx235中的烘烤电缆”,“字123 456”)
gsub((?我们也可以使用str\u replace
library(stringr)
str_replace_all(text, "(?<=[[:alpha:]]) (?=\\d+)", "")
#[1] "There is a cooling problem in the component tx313" "leakage found in irc256, fixed by replacement" "Roasted cable in cpt trx235"
#[4] "word123 456"
库(stringr)
str_replace_all(text,”(?我们也可以使用str_replace
library(stringr)
str_replace_all(text, "(?<=[[:alpha:]]) (?=\\d+)", "")
#[1] "There is a cooling problem in the component tx313" "leakage found in irc256, fixed by replacement" "Roasted cable in cpt trx235"
#[4] "word123 456"
库(stringr)
str_replace_all(text),(?您好,欢迎来到StackOverflow,如果您能告诉我们您是如何做到这一点并重新表述您的问题(以避免得到像“是”或“否”这样的答案),那就太好了。您如何定义“单词”?它可以由哪些字符组成?任何非空白符号,或者与\w
(字母/数字/\uu
)?关于word123456
,它应该变成word123456
还是word123456
?你可以试试:gsub(“(?在这种情况下,一个单词只是字母的组合,同样,一个数字是数字的组合,word123456
应该变成word123456
谢谢@Haboryme!太完美了。你能解释一下你是怎么做到的吗?你好,欢迎来到StackOverflow,如果你能告诉我们你是怎么做到的,那就太好了到目前为止,请不要重复您的问题(以避免出现“是”或“否”这样的答案)。您如何定义“单词”?它可以由哪些字符组成?任何非空白符号,或与\w
匹配的符号(字母/数字/
)?关于word123456
,它应该变成word123456
还是word123456
?你可以试试:gsub(“(?在这种情况下,一个单词只是字母的组合,同样地,一个数字是数字的组合,word123456
应该变成word123456
谢谢@Haboryme!太完美了。你能解释一下你是怎么做的吗?我相信gsub
是以R为基数的。是的!但我想?@joel.wils在此基础上,通过指定perl=TRUE
很好的解释和答案@Haboryme+1,可以在所有基本正则表达式函数中使用它。我发现了一个它不起作用的情况,例如在20分钟后检查了trx 250
预期结果是在20分钟后检查了trx250
。感谢您的修改,我相信gsub
在base R.是的!但我认为?@joel.wilson这可以通过指定perl=TRUE
很好的解释和答案@Haboryme+1在所有基本正则表达式函数中使用。我发现了一个它不起作用的情况,例如在20分钟后检查了trx 250
预期结果是在20分钟后检查了trx250
。感谢您的帮助he修饰