I'm trying to extract substrings when it matches certain pattern. For example:
str <- "For each of the following statements, please indicate how true it is for\r\nyou with respect to your interaction with the puzzles in the game. - This is the part of string I want to extract."
str_extract(str, pattern = "(?<=-)\\w ") #Output = This
How do I get the whole string?
CodePudding user response:
If we want to extract the whole sentence till the .
, match for one or more characters that are not a .
([^.]
) followed by the .
(\\.
- escape as it is metacharacter that matches any character) after the regex lookaround to match the -
and a space (\\s
)
library(stringr)
str_extract(str, pattern = "(?<=-\\s)[^.] \\.")
[1] "This is the part of string I want to extract."