简体   繁体   English

如何使用R从字符串中删除所有数字和逗号(除了任何紧跟$的数字)?

[英]How to remove all numbers and commas from a string except any number immediately preceded by $ using R?

I would like to remove all numbers and commas from a string except any number that is immediately preceded by $ and immediately followed by a comma. 我想从字符串中删除所有数字和逗号,除了任何紧跟$且紧跟逗号的数字。

For example, I have: 例如,我有:

str = "1, $100-$1,000 2, $1001-$10,000 3, $10,001-$100,000"

I would like to obtain the following: 我想获得以下内容:

"$100-$1,000  $1001-$10,000  $10,001-$100,000"

I have tried to use gsub with a negative lookbehind 我试图将gsub用作反面

new_str = gsub("(?<!\\$)[0-9]*,", "", str)

However, this gives the following error message: 但是,这给出了以下错误消息:

Error in gsub("(?<!\\$)[0-9]*,", "", str) : invalid regular expression '(<!\$)[0-9]*,', reason 'Invalid regexp'

It seems that the negative lookbehind is incorrectly coded, but I can't seem to figure out why. 负向后看似被错误地编码,但我似乎无法弄清楚为什么。 Any help is much appreciated! 任何帮助深表感谢!

1) This gives the desired answer in the case of the sample string: 1)对于示例字符串,这给出了所需的答案:

gsub("\\d+, ", "", str)
## [1] "$100-$1,000 $1001-$10,000 $10,001-$100,000"

Visualization of regular expression 可视化正则表达式

\d+, 

正则表达式可视化

Debuggex Demo Debuggex演示

2) Here is a second approach: 2)这是第二种方法:

library(gsubfn)

paste(strapplyc(str, "(\\$\\S+)", simplify = c), collapse = " ")
## [1] "$100-$1,000 $1001-$10,000 $10,001-$100,000"

Visualization of regular expression 可视化正则表达式

(\$\S+)

正则表达式可视化

Debuggex Demo Debuggex演示

you could use this pattern 你可以使用这种模式

(\$[0-9,-]+)|\d+,\s 

and replace w/ \\1 并替换w / \\1
Demo 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串中删除除数字和逗号之外的所有字符? - Remove all characters from a string except numbers and commas? 如何从字符串中删除除数字、“,”和“.”之外的所有字符使用红宝石? - How to remove all characters from string except numbers, "," and "." using Ruby? JavaScript:如何从字符串中删除所有包含(或紧随其前)大写字母,数字或逗号的单词? - JavaScript: How can I remove any words containing (or directly preceding) capital letters, numbers, or commas, from a string? 如何删除字符串中除“1”和“2”之外的所有数字? - How to remove all number in string except “1” and “2”? 从R中的字符串正则表达式中删除除句点和数字之外的所有内容 - Remove everything except period and numbers from string regex in R 删除所有数字,除了使用 python regex 组合成字符串的数字 - Remove all numbers except for the ones combined to string using python regex 如何删除任何字符串末尾的逗号 - How to remove commas at the end of any string 正则表达式,查找除前面带有字母的数字之外的任何数字 - Regex, Find any number except for the number preceded by a letter 从字符串中删除数字,除非前面有字母 - remove the numbers from a string, except if there is a letter before 如何从字符串中去除所有字符(数字,空格和+除外) - How to strip all characters from a string except for numbers, space and +
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM