匹配多个模式 [英] Matching multiple patterns
问题描述
我想看看,如果001
或100
或 000
出现在 0
和 1
4个字符的字符串中。例如,一个4字符的字符串可以像1100
或0010
或 1001
或1111
。如何用单个命令匹配字符串中的多个字符串?
我知道grep可以用于模式匹配,但是使用grep,我一次只能检查一个字符串。我想知道是否有多个字符串可以与其他命令一起使用或与grep本身一起使用。
是的,你可以。 grep
模式中的 |
与或
。所以你可以使用001 | 100 | 000
作为你的模式来测试你的模式。同时, grep
被矢量化,因此所有这些都可以一步完成: x <-c(1100,0010,1001,1111)
pattern < - 001 | 100 | 000
grep(pattern,x)
[1] 1 2 3
你的向量中包含匹配模式的索引(在本例中是前三个)。
有时,有一个逻辑向量可以更方便地告诉你哪个你矢量中的元素是匹配的。然后你可以使用 grepl
:
grepl(pattern,x)
[1] TRUE TRUE TRUE FALSE
请参阅?regex
获取有关R中正则表达式的帮助。
编辑:
为了避免手动创建模式,我们可以使用 paste
:
myValues < - c(001,100,000)
pattern< - paste(myValues,collapse =|)
I want to see, if "001"
or "100"
or "000"
occurs in a string of 4 characters of 0
and 1
. For example, a 4 character string could be like "1100"
or "0010"
or "1001"
or "1111"
. How do I match many strings in a string with a single command?
I know grep could be used for pattern matching, but using grep, I can check only one string at a time. I want to know if multiple strings can be used with some other command or with grep itself.
Yes, you can. The |
in a grep
pattern has the same meaning as or
. So you can test for your pattern by using "001|100|000"
as your pattern. At the same time, grep
is vectorised, so all of this can be done in one step:
x <- c("1100", "0010", "1001", "1111")
pattern <- "001|100|000"
grep(pattern, x)
[1] 1 2 3
This returns an index of which of your vectors contained the matching pattern (in this case the first three.)
Sometimes it is more convenient to have a logical vector that tells you which of the elements in your vector were matched. Then you can use grepl
:
grepl(pattern, x)
[1] TRUE TRUE TRUE FALSE
See ?regex
for help about regular expressions in R.
Edit:
To avoid creating pattern manually we can use paste
:
myValues <- c("001", "100", "000")
pattern <- paste(myValues, collapse = "|")
这篇关于匹配多个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!