如何在字符串R中的两个单词之间获取多个文本?

时间:2016-10-25 05:55:10

标签: r regex string gsub

我试图在一个句子中的两个单词之间得到文本。 例如,句子是 -

x <-  "This is my first sentence and This is my second sentence"

现在我想输出如下:

[1] first second

这是我尝试过的,但它不起作用

gsub('^.*This is my \\s*|\\s*sentence.*$', '', x)

1 个答案:

答案 0 :(得分:1)

试试这个:

x <-  "This is my first sentence and This is my second sentence"
gsub('.*?This is my (\\w*?) sentence.*?', '\\1 ', x)