如何使用R代码找出句子中的连续单词

时间:2015-06-17 11:36:06

标签: regex r

如何使用R代码查找句子中的连续单词。

例如:

下面有一个如下所述的句子,它是以下

的输出
sentence <- text[grep("Guarantee of",text)]

&#34;您需要提交13,863.00卢比的性能保证/(卢比一万三千八百六十三)&#34;

现在我需要获得&#34;保证&#34; 的连续字样,其中&#34; Rs.13,863.00 / - &#34;

-Thanks

2 个答案:

答案 0 :(得分:1)

sentence <- 'You are requested to submit the Performance Guarantee of Rs.13,863.00/-( Rupees thirteen thousand and eight sixty three)';
sub('.*Guarantee\\s+of\\s+([a-zA-Z0-9,._/-]+).*','\\1',sentence);
## [1] "Rs.13,863.00/-"

答案 1 :(得分:1)

尝试

gsub('.*Guarantee of\\s*|\\(.*', '', str1)
[1] "Rs.13,863.00/-"

或者

library(stringr)
str_extract(str1, '(?:Rs.)[^(]+')
#[1] "Rs.13,863.00/-"

数据

  str1 <- "You are requested to submit the Performance Guarantee of Rs.13,863.00/-( Rupees thirteen thousand and eight sixty three)"