用元字符在R中拆分 [英] strsplit in R with a metacharacter
问题描述
我有大量的数据,其中的定界符是反斜杠.我正在R中处理它,而且由于反斜杠是一个元字符,因此很难找到如何分割字符串的方法.例如,一个字符串如下所示:
I have a large amount of data where the delimiter is a backslash. I'm processing it in R and I'm having a hard time finding how to split the string since the backslash is a metacharacter. For example, a string would look like this:
1128\0019\XA5\E2R\366\00=15
,我想将其沿\
字符分割,但是当我运行strsplit命令时:
and I want to split it along the \
character, but when I run the strsplit command:
strsplit(tempStr, "\\")
Error in strsplit(tempStr, "\\") :
invalid regular expression '\', reason 'Trailing backslash'
当我尝试使用"fixed"选项时,它无法运行,因为它在反斜杠后期望有一些东西:
When I try to used the "fixed" option, it does not run because it is expecting something after the backslash:
strsplit(tempStr, "\", fixed = TRUE)
不幸的是,我无法使用其他程序对数据进行预处理,因为该数据是每天生成的.
Unfortunately, I can't preprocess the data with another program because the data is generated daily.
推荐答案
您的代码行是(尽管您没有明确说明):
Your line of code is (though you don't say it explicitly):
strsplit(tempStr, "\\")
并且应该是
strsplit(tempStr, "\\\\")
这将被读为"\\",正则表达式将被理解为"\".
This will be read as "\\", which the regular expression will understand as a "\".
这篇关于用元字符在R中拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!