gpt4 book ai didi

regex - R 正则表达式/gsub : How to collapse spaces in a string

转载 作者:行者123 更新时间:2023-12-04 15:25:12 25 4
gpt4 key购买 nike

我有一个从手写文档中扫描出来的句子向量。在这个过程中出现了一些像这样的间距问题:

 The d og is br own.

我很好奇是否有一种方法可以用 '_x_' 一般地采用任何模式或 space-character-space 并像这样折叠第二个空格:
The d og is br own.  --> The dog is br own.

我只担心空格之间的单个字符( '_x_' 不是 '_xx_' )。

有什么建议?

最佳答案

也许

> x<-"The d og is br own."
> gsub(" (.) "," \\1",x)
[1] "The dog is br own."

或者
gsub(" ([[:alnum:]]) "," \\1",x)
(.)匹配任何内容 ([[:alnum:]])仅匹配字母数字字符。

关于regex - R 正则表达式/gsub : How to collapse spaces in a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11572202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com