Lambo
Lambo

Reputation: 897

regular expression find strings with certain pattern in R

I have some strings here and they are:

12ABC3, 2ABC45, ABC 56, uhyABC, REGEXP ...

The objective is as long as there is 'ABC' in a string (not 'BCA' or 'BAC') it should return TRUE when using 'grepl'

So the output should be

TRUE, TRUE, TRUE, TRUE, FALSE

Can anybody help me with this?

Thanks in advance

Upvotes: 4

Views: 5687

Answers (3)

hwnd
hwnd

Reputation: 70722

You can use the following.

> x <- c('12ABC3', '2ABC45', 'ABC 56', 'uhyABC', 'REGEXP')
> grepl('ABC', x, fixed=T)
# [1]  TRUE  TRUE  TRUE  TRUE FALSE
> x[grepl('ABC', x, fixed=T)]
# [1] "12ABC3" "2ABC45" "ABC 56" "uhyABC"

Upvotes: 3

Rich Scriven
Rich Scriven

Reputation: 99321

You want to use fixed = TRUE in your call to grepl.

> x <- c("12ABC3", "2ABC45", "ABC 56", "uhyABC", "REGEXP", "BCA", "CAB")
> grepl("ABC", x, fixed = TRUE)
# [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE

The fixed argument definition is

logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

Upvotes: 6

Dalorzo
Dalorzo

Reputation: 20014

How about this expression: \w*ABC[\w\s]*

Online Demo

\w* match any word character [a-zA-Z0-9_] * will make it match from 0 to unlimited times.

\w\s]* match any word character [a-zA-Z0-9_ ] this one includes space in your forth expression and again * will make it match from 0 to unlimited times.

Upvotes: 1

Related Questions