user3375672
user3375672

Reputation: 3768

R, stringr::str_extract_all: Get all occurences specified in regex list

Really simple, but I can get the 'greediness' of regex to work like I want. Say you have:

unlist(stringr::str_extract_all("XXXXSXTXXX","([A-Z]{2}[T|S][A-Z]{2})"))

This gives only the first match:

[1] "XXSXT"

How can I change the regex behaviour to give me both matches with S and T (without use two separate patterns), like:

[1] "SXTXX" "XXSXT" 

Upvotes: 1

Views: 270

Answers (1)

vks
vks

Reputation: 67968

You need to use lookahead for that with perl=True option for match in R.

(?=([A-Z]{2}[TS][A-Z]{2}))

See demo.

https://regex101.com/r/cJ6zQ3/23

Upvotes: 1

Related Questions