We can use \\S+
as pattern to match one or more non-whitespace character
library(stringr)
str_extract(str1, "\\S+\\s+\\S+\\s+\\S+")
#[1] "*** CONNECTION WAS"
str_extract(str2, "\\S+\\s+\\S+\\s+\\S+")
#[1] "some random example"
str_extract(str3, "\\S+\\s+\\S+\\s+\\S+")
#[1] "response message: <?xml"
According to ?regex
The symbol \w matches a ‘word’ character (a synonym for [[:alnum:]_], an extension)
Thus, it matches only the alphanumeric characters along with _
and not *
. Also, the *
in regex implies metacharacter that signifies zero or more
data
str1 <- "*** CONNECTION WAS MADE IN MESSAGE ***"
str2 <- "some random example message"
str3 <- 'response message: <?xml version="1.0" encoding'
CLICK HERE to find out more related problems solutions.