以特定模式开头的字符串 [英] string starting with specific pattern
问题描述
这是一个非常简单的 R 问题,但我找不到答案.
This is very simple R question but I could not find an answer.
我想查找以特定模式开头的字符串.例如如果我有图案"ABC
我想在以下向量中提取以 ABC
开头的字符串c("ABCGDFGFD","WWABC","AYBC")
I would like to find strings starting with a specific pattern.
e.g. if I have the pattern
"ABC
and I would like extract strings starting with ABC
in the following vector
c("ABCGDFGFD","WWABC","AYBC")
此示例的预期结果:TRUE FALSE FALSE
注意:在这个例子中,模式的长度并不总是 3 个字符.它可以更长或更短.
NB: The length of the pattern is not always 3 characters as in this example. It could be longer or shorter.
提前致谢!
推荐答案
Regex 在这种情况下更简单:
Regex is simpler in this case:
grepl("^ABC", x)
[1] TRUE FALSE FALSE
插入符号 ^
特殊字符标识行的开头.无需指定要计数的字符数.
The caret ^
special character identifies the beginning of the line. No need to have to specify the amount of characters to count to.
这篇关于以特定模式开头的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!