Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching multiple patterns

Tags:

regex

r

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.

like image 272
Narayani Avatar asked Aug 04 '11 19:08

Narayani


People also ask

What is multiple pattern matching?

The multi-pattern matching problem consists in finding all occurrences of the patterns from a finite set X in a given text T of. length n. We present a new and simple algorithm combining the ideas of the Aho–Corasick algorithm and the directed acyclic. word graphs.

How do I combine multiple regex patterns?

to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?

What is regex matching pattern?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

Is there pattern matching in Java?

Pattern matching is a feature that is still being worked on. Some elements of this feature have been released as final features in the Java language, some have been released as preview features, and some are still being discussed.


2 Answers

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 = "|") 
like image 118
Andrie Avatar answered Sep 18 '22 16:09

Andrie


Here is one solution using stringr package

require(stringr) mylist = c("1100", "0010", "1001", "1111") str_locate(mylist, "000|001|100") 
like image 21
Ramnath Avatar answered Sep 20 '22 16:09

Ramnath