0

I need to capture the third instance of a pattern using regex. My code works using the online R Regex Tester, but not with the stringr::str_extract() function.

What is the reason for this?

string = c("W901 Z846 W903 Z846 W919 Z846 Z941")

str_extract(string, "^(?:.*?Z84[1-9]{1}){2}(.*?Z84[1-9]{1})")

# [1] "W901 Z846 W903 Z846 W919 Z846"

Result should be: "W919 Z846".

See this R Regex Tester result where the correct result is returned.

1
  • It works with strcapture in base R: strcapture(pat, string, list(x = character())) Commented Oct 5 at 0:55

1 Answer 1

0

The regex captures a group. I realised I can display this captured group using the group = 1 option in the `str_extract()` function

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.