Ahsan Nawaz
Ahsan Nawaz

Reputation: 43

Split string using space and capital letter

I'm trying to split my string into multiple rows. String looks like this:

x <- c("C 10.1 C 12.4","C 12", "C 45.5 C 10")  

Code snippet:

strsplit(x, "//s")[[3]]

Result:

"C 45.5 C 10"

Expected Output: Split string into multiple rows like this:

"C 10.1"
"C 12.4"
"C 12"
"C 45.5"
"C 10" 

The question is how to split the string?

Clue: there is a space and then character which is "C" in our case. Anyone who knows how to do it?

Upvotes: 2

Views: 379

Answers (2)

s_baldur
s_baldur

Reputation: 33498

A somwhat more complicated expression but easier on the regex side:

unlist(
  sapply(
    strsplit(x, " ?C"),
    function(x) {
      paste0("C", x[nzchar(x)])
    }
  )
)
"C 10.1" "C 12.4" "C 12"   "C 45.5" "C 10"  

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

You may use

unlist(strsplit(x, "(?<=\\d)\\s+(?=C)", perl=TRUE))

Output:

[1] "C 10.1" "C 12.4" "C 12"   "C 45.5" "C 10" 

See the online R demo and a regex demo.

The (?<=\\d)\\s+(?=C) regex matches 1 or more whitespace characters (\\s+) that are immediately preceded with a digit ((?<=\\d)) and that are immediately followed with C.

If C can be any uppercase ASCII letter, replace C with [A-Z].

Upvotes: 3

Related Questions