Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (50.2k points)

I read about regex and came across word boundaries. Using the below code I am not having the desired output. Here:

grep("\\bcat\\b", "The cat scattered his food all over the room.", value= TRUE)

# I expect "cat" but it returns the whole string.

grep("\\B-\\B", "Please enter the nine-digit id as it appears on your color - coded pass-key.", value= TRUE)

# I expect "-" but it returns the whole string.

What am I doing wrong?

1 Answer

0 votes
by (108k points)

In R programming for getting the desired output, you can use the regexpr() and the regmatches() to get the match. grep gives where it hits. You can also use the sub.

x <- "The cat scattered his food all over the room."

regmatches(x, regexpr("\\bcat\\b", x))

#[1] "cat"

sub(".*(\\bcat\\b).*", "\\1", x)

#[1] "cat"

x <- "Please enter the nine-digit id as it appears on your color - coded pass-key."

regmatches(x, regexpr("\\B-\\B", x))

#[1] "-"

sub(".*(\\B-\\B).*", "\\1", x)

#[1] "-"

For more than one match, you can use the gregexpr():

x <- "1abc2"

regmatches(x, gregexpr("[0-9]", x))

#[[1]]

#[1] "1" "2"

Related questions

Browse Categories

...