user3973290
user3973290

Reputation:

string splitting based on delimiters

My string is

 c("closely-monitored",  "rights-of-way", "THIS_IS_A_CONSTANT")

I like to split this to separate words as

  "closely" "-monitored", "rights", "-of", "-way", "THIS", "_IS", "_A", "_CONSTANT"

Tried

 paste("\\-", strsplit(str3, "_|-"))

but it is not working.

Upvotes: 2

Views: 93

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

Here's an approach with perl-compatible regex:

vec <- c("closely-monitored",  "rights-of-way", "THIS_IS_A_CONSTANT")

unlist(strsplit(vec, "(?<=[^-_])(?=[-_])", perl = TRUE))

# [1] "closely"    "-monitored" "rights"     "-of"        "-way"      
# [6] "THIS"       "_IS"        "_A"         "_CONSTANT" 

Upvotes: 3

Related Questions