如何删除unicode< U + 00A6>从字符串? [英] How to remove unicode <U+00A6> from string?
本文介绍了如何删除unicode< U + 00A6>从字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的字符串:
I have a string like:
q <-"<U+00A6> 1000-66329"
我要删除<U+00A6>
并仅获取1000 66329
.
我尝试使用:
gsub("\u00a6"," ", q,perl=T)
但是它没有删除任何东西.我应该怎么做gsub
才能只得到1000 66329
?
But it is not removing anything. How should I do gsub
in order to get only 1000 66329
?
推荐答案
我只想删除字符串开头的unicode
<U+00A6>
.
然后您不需要gsub
,则可以将sub
与"^\\s*<U\\+\\w+>\\s*"
模式一起使用:
Then you do not need a gsub
, you can use a sub
with "^\\s*<U\\+\\w+>\\s*"
pattern:
q <-"<U+00A6> 1000-66329"
sub("^\\s*<U\\+\\w+>\\s*", "", q)
模式详细信息:
-
^
-字符串开头 -
\\s*
-零个或多个空格 -
<U\\+
-文字字符序列<U+
-
\\w+
-1个或多个字母,数字或下划线 -
>
-文字>
-
\\s*
-零个或多个空格.
^
- start of string\\s*
- zero or more whitespaces<U\\+
- a literal char sequence<U+
\\w+
- 1 or more letters, digits or underscores>
- a literal>
\\s*
- zero or more whitespaces.
如果您还需要用空格替换-
,请添加|-
替代项并使用gsub
(因为现在我们期望多个替换项,并且替换项必须是空格-在akrun的答案):
If you also need to replace the -
with a space, add |-
alternative and use gsub
(since now we expect several replacements and the replacement must be a space - same is in akrun's answer):
trimws(gsub("^\\s*<U\\+\\w+>|-", " ", q))
请参见 R在线演示
这篇关于如何删除unicode< U + 00A6>从字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文