我有一个像这样的字符串:
q <-"<U+00A6> 1000-66329"
我想删除
<U+00A6>
并仅获取1000 66329
。我尝试使用:
gsub("\u00a6"," ", q,perl=T)
但这并没有删除任何内容。我应该怎么做
gsub
才能只获得1000 66329
?
最佳答案
I just want to remove unicode
<U+00A6>
which is at the beginning of string.
然后,您不需要
gsub
,可以将sub
与"^\\s*<U\\+\\w+>\\s*"
模式一起使用:q <-"<U+00A6> 1000-66329"
sub("^\\s*<U\\+\\w+>\\s*", "", q)
模式详细信息:
^
-字符串\\s*
-零个或多个空格<U\\+
-文字字符序列<U+
\\w+
-1个或多个字母,数字或下划线>
-文字>
\\s*
-零个或多个空格。 如果还需要用空格替换
-
,请添加|-
替代品并使用gsub
(因为现在我们希望多次替换,并且替换必须是一个空格-akrun's answer中也是如此):trimws(gsub("^\\s*<U\\+\\w+>|-", " ", q))
见R online demo
关于r - 如何从字符串中删除unicode <U + 00A6>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993715/