user1836529
user1836529

Reputation: 209

Regular expression in R

I am having some troubles with the regular expression in R. I use str_extract from the library stringr and my problem is :

library(stringr)
test="word1 something word2 something word3 something word3"
temp = str_extract(test,'word2.+word3')
print(temp)
## [1] "word2 something word3 something word3"

The problem is that I want it to stop at the first word3, I don't want the last part of the string. Any idea please ? thank you very much

and if I have

test="word1 something word2 something1 word3 something2 word3 something3 word2 something4 word3"

and that I want to keep a 2 size vector like this "word2 something1 word3", "word2 something4 word3" thanks again

Upvotes: 3

Views: 906

Answers (3)

Onyambu
Onyambu

Reputation: 79198

using base r: We can capture all the output before word3 by using a backreference

 sub("(word3).*","\\1",test)
 [1] "word1 something word2 something word3"

Upvotes: 0

Tyler Rinker
Tyler Rinker

Reputation: 109844

I think that you're trying to extract every occurrence between two points in a string. If I'm wrong my apologies. This can be accomplished with qdap's genXtract and setting with = TRUE. Also this is not a stringr answer:

test="word1 something word2 something1 word3 something2 word3 something3 word2 something4 word3"

library(qdap)
genXtract(test, left = "word2", right = "word3", with=TRUE)

## > genXtract(test, "word2", "word3", with=TRUE)
##         word2  :  word31         word2  :  word32 
## "word2 something1 word3" "word2 something4 word3" 

Upvotes: 3

Jerry
Jerry

Reputation: 71538

Change your regex line to:

temp = str_extract(test,'word2.+?word3')
                                ^

Notice that I added ? which makes the .+ non greedy (i.e. it captures as little as possible as opposed to capture everything before the next term in the regex).

To extract all the occurrences, use:

temp = str_extract_all(test,'word2.+?word3')

Upvotes: 12

Related Questions