替换数字后面的句点 [英] Replace period following a digit
问题描述
我遇到了一个看似简单的问题,但我找到的解决方案并不令人满意.我想用逗号替换任何后跟数字的句点.例如,
I've come across a problem that seems simple, yet solution I've found isn't satisfying. I want to replace any period followed by a number with a comma. For example,
strings <- c("22.222.222", "12.323", "don.t replace")
会成功转化为
[1] "22,222,222" "12,323" "don.t replace"
我尝试的第一个解决方案是
The first solution I attempted was
str_replace_all(strings,
"(?<=\\d+)\\.",
",")
但我收到以下错误消息:
but I got the following error message:
Error in stri_replace_all_regex(string, pattern, replacement, vectorize_all = vec, :
Look-Behind pattern matches must have a bounded maximum length. (U_REGEX_LOOK_BEHIND_LIMIT)
当我使用
str_replace_all(strings,
"(?<=\\d{1,3})\\.",
",")
我得到了正确的转换.但是,硬编码的 1-3 位数字不是我想要的,我不明白为什么 \\d+
不起作用但 \\d{1,3}代码>确实如此.
I get the correct transformation. However, the hard-coded 1-3 digits is not what I want and I don't understand why \\d+
doesn't work but \\d{1,3}
does.
推荐答案
不需要 +
量词,您只关心匹配序列中的最后一位数字.所以只需将 \d
放在lookbehind 中.
There's no need for the +
quantifier, all you care about is matching the last digit in the sequence. So just put \d
in the lookbehind.
str_replace_all(strings,
"(?<=\\d)\\.",
",")
这篇关于替换数字后面的句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!