BrooklynSon
BrooklynSon

Reputation: 2103

R Regular Expression using "+" "?" or "*"

From https://stringr.tidyverse.org/articles/regular-expressions.html

It describes controlling how many times a pattern shows up using:

So this works well:

str_view("hello123world", "123?")

However why not this?

str_view("cycyccyccccc", "ccc?")

The above highlights cc in the beginning of the string.

I was expecting it to highlight three c's in the last rows of c's at the end.

Upvotes: 1

Views: 73

Answers (1)

Greg
Greg

Reputation: 3660

The regex you passed "ccc?" means (in words) "c followed by c followed by zero or one c's" so str_view gives you the first instance of two c's in a row (because that's c followed by c followed by zero c's).

If you'd like exactly 3 c's you could use

str_view("cycyccyccccc", "ccc")

Or if you'd like 3 or more c's you could use

str_view("cycyccyccccc", "c{3,}")

Upvotes: 8

Related Questions