首页 > 解决方案 > 从列中删除所有数字

问题描述

使用它可以删除 nbsp

str_replace_all(df$text, 'nbsp', '')

有人可以使用哪种正则表达式通过此命令删除所有数字?

标签: rregex

解决方案


如果通过“nbsp”您指的是非中断空间,那么它应该通过使用显式 Unicode 编码来工作。

nbsp以0x00A0Unicode编码,因此在 R 上您可以将其表示为"\U00a0".

例如:

> "This is a strange\U00A0 character"
[1] "This is a strange  character"

在此处输入图像描述

使用不同的字符可能会更清楚:

> "This is a strange \U00A1 character"
[1] "This is a strange ¡ character"

在此处输入图像描述

这可以像您期望的那样被删除。

> str_remove("This is a strange \U00A1 character", "\U00A1")
[1] "This is a strange  character"
> str_remove("This is a strange\U00A0 character", "\U00A0")
[1] "This is a strange character"

这也可以通过提供十进制表示法来工作:

str_remove("This is a strange\U00A0 character", intToUtf8(160))

请注意,这适用于我的计算机,但安装的区域设置和字体可能会有所不同。


推荐阅读