gpt4 book ai didi

regex - 将标点符号转换为空格

转载 作者:行者123 更新时间:2023-12-02 10:56:18 25 4
gpt4 key购买 nike

我有一堆带标点符号的字符串,我想将其转换为空格:

"This is a string. In addition, this is a string (with one more)."

会变成:

"This is a string  In addition  this is a string  with one more  "

我可以使用 stringr 包 (str_replace_all()) 手动执行此操作,一次一个标点符号 (,/./!/(/)/等),但我很好奇是否有更快的方法,我假设使用正则表达式。

有什么建议吗?

最佳答案

x <- "This is a string. In addition, this is a string (with one more)."
gsub("[[:punct:]]", " ", x)
[1] "This is a string In addition this is a string with one more "

请参阅 ?gsub 进行此类快速替换,并参阅 ?regex 了解有关 [[:punct:]] 类的详细信息,即

‘[:punct:]’ Punctuation characters:
‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { |
} ~’.

关于regex - 将标点符号转换为空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11498157/

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