问题描述
我在"euro"变量中保存了一个欧元符号:
I have a euro symbol saved in "euro" variable:
euro <- "\u20AC"
euro
#[1] "€"
"eurosearch"变量包含此SOW中定义的服务,价格为15,896.80欧元(如果来回执行).
And "eurosearch" variable contains "services as defined in this SOW at a price of € 15,896.80 (if executed fro" .
eurosearch
[1] "services as defined in this SOW at a price of € 15,896.80 (if executed fro"
我希望欧元符号后的字符为"15,896.80(如果来回执行)"我正在使用此代码:
I want the characters after the Euro symbol which is "15,896.80 (if executed fro"I am using this code:
gsub("^.*[euro]","",eurosearch)
但是我得到的结果是空的.如何获得预期的输出?
But I'm getting empty result. How can I obtain the expected output?
推荐答案
您可以通过使用 paste0 :
euro <- "€"
eurosearch <- "services as defined in this SOW at a price of € 15,896.80 (if executed fro"
sub(paste0("^.*", gsub("([^A-Za-z_0-9])", "\\\\\\1", euro), "\\s*(\\S+).*"), "\\1", eurosearch)
euro <- "$"
eurosearch <- "services as defined in this SOW at a price of $ 25,196.4 (if executed fro"
sub(paste0("^.*", gsub("([^A-Za-z_0-9])", "\\\\\\1", euro), "\\s*(\\S+).*"), "\\1", eurosearch)
请参见 CodingGround演示
请注意,使用gsub("([^A-Za-z_0-9])", "\\\\\\1", euro)
时,我会转义任何非单词符号,以便将$
视为文字,而不是特殊的正则表达式元字符(取自).
Note that with gsub("([^A-Za-z_0-9])", "\\\\\\1", euro)
I am escaping any non-word symbols so that $
could be treated as a literal, not a special regex metacharacter (taken from this SO post).
这篇关于删除R中EURO符号后的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!