Learn R Programming

udpipe (version 0.8.11)

txt_contains: Check if text contains a certain pattern

Description

Look up text which has a certain pattern. This pattern lookup is performed by executing a regular expression using grepl.

Usage

txt_contains(x, patterns, value = FALSE, ignore.case = TRUE, ...)

Value

a logical vector of the same length as x indicating if one of the patterns was found in x.

Or the vector of elements of x where the pattern was found in case argument value is set to TRUE

Arguments

x

a character vector with text

patterns

a regular expression which might be contained in x, a vector of these or a list of pattern elements where the list elements include and exclude indicate to find a pattern in x while excluding elements which have another pattern

value

logical, indicating to return the elements of x where the pattern was found or just a logical vector. Defaults to FALSE indicating to return a logical.

ignore.case

logical, if set to FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching. Passed on to grepl

...

other parameters which can be passed on to grepl e.g. fixed/perl/useBytes

See Also

Examples

Run this code
x <- c("The cats are eating catfood", 
       "Our cat is eating the catfood", 
       "the dog eats catfood, he likes it", 
       NA)
txt_contains(x, patterns = c("cat", "dog")) 
txt_contains(x, patterns = c("cat", "dog"), value = TRUE) 
txt_contains(x, patterns = c("eats"), value = TRUE) 
txt_contains(x, patterns = c("^The"), ignore.case = FALSE, value = TRUE) 
txt_contains(x, patterns = list(include = c("cat"), exclude = c("dog")), 
             value = TRUE) 
txt_contains(x, "cat") & txt_contains(x, "dog")

Run the code above in your browser using DataLab