Home > Software engineering >  Get the word before exclamation mark in R tidyverse
Get the word before exclamation mark in R tidyverse

Time:12-16

I´m wondering how to get the words that occur before an exclamation mark! I have a dataframe with different strings on each row. I have tried following:

text %>%
    str_match("!",lines)

I don´t really get what I want and I´m a bit lost. Anyone has advice?

CodePudding user response:

You can str_extract_all the words before the ! using lookahead:

Data:

text <- c("Hello!", "This a test sentence", "That's another test sentence, yes! It is!", "And that's one more")

Solution:

library(stringr)   
unlist(str_extract_all(text, "\\b\\w \\b(?=!)"))
[1] "Hello" "yes"   "is" 

If you seek a dplyr solution:

data.frame(text) %>%
  mutate(Word_before_excl = str_extract_all(text, "\\b\\w \\b(?=!)"))
                                       text Word_before_excl
1                                    Hello!            Hello
2                      This a test sentence                 
3 That's another test sentence, yes! It is!          yes, is
4                       And that's one more                 

CodePudding user response:

Maybe we can use regmatches

> sapply(regmatches(text, gregexpr("\\b\\w \\b(?=!)", text, perl = TRUE)), toString)
[1] "Hello"   ""        "yes, is" ""

CodePudding user response:

You could also use :

> unlist(strsplit("Dog!Cat", "!"))[1]
[1] "Dog"
  • Related