R中字符列表中模式的位置

问题描述 投票:-1回答:2

我想要做的是在字符列表中搜索特定模式并返回位置,以便稍后我可以将它们排除。

我的数据是一个文本,其中每个单词都附有一个词性标记,格式为:

test
$text
[1] "This/DT is/VBZ a/DT short/JJ sentence/NN ,/, to/TO test/VB if/IN everything/NN is/VBZ working/VBG ./."
$POStags
 [1] "DT"  "VBZ" "DT"  "JJ"  "NN"  "$,"   "TO"  "VB"  "IN"  "NN"  "VBZ"
[12] "VBG" "$."  

我想过滤所有出现的“$”。和“$”。我尝试过以下方法:

grep("$.", test$POStags, value = TRUE)

返回character(0)

我是R的新手并且觉得应该有一个简单的解决方案,但不知怎的,我无法让它工作......提前感谢任何帮助!

r regex pattern-matching
2个回答
1
投票
grep("\\$(\\.|,)", test$POStags)
[1]  6 13

1
投票

这个怎么样

which(grepl("$.",test$POStags,fixed = T) | grepl("$,",test$POStags,fixed = T))
© www.soinside.com 2019 - 2024. All rights reserved.