Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r mask for grep for finding the repeated words

Tags:

regex

r

I just want find rows with the repeated word HALL (more than one time). For example, "HALL #1 HALL #2 HALL #3". I tried to use

grepl("HALL{2,}", "HALL #1 HALL #2 HALL #3")

but grepl returned FALSE. What am I doing wrong?

like image 232
Sergey Bolotin Avatar asked Jun 07 '16 14:06

Sergey Bolotin


2 Answers

You can use stringr,

str_count("HALL #1 HALL #2 HALL #3", 'HALL')>1
#[1] TRUE
like image 134
Sotos Avatar answered Sep 22 '22 11:09

Sotos


You could use (?:.*?HALL.*?){2,}:

grepl("(?:.*?HALL.*?){2,}", "HALL #1 HALL #2 HALL #3")
#[1] TRUE

Here is a breakdown of the above regex.

like image 25
nrussell Avatar answered Sep 18 '22 11:09

nrussell