Roman
Roman

Reputation: 17678

Split string after comma without trailing whitespace

As the title already says, I want to split this string

strsplit(c("aaa,aaa", "bbb, bbb", "ddd , ddd"), ",")

to that

[[1]]
[1] "aaa" "aaa"

[[2]]
[1] "bbb, bbb"

[[3]]
[1] "ddd , ddd"

Thus, the regular expression has to consider that no whitespace should occur after the comma. Could be a dupe, but was not able to find a solution by googling.

Upvotes: 3

Views: 1302

Answers (2)

Jan
Jan

Reputation: 43199

Just to provide an alternative using (*SKIP)(*FAIL):

pattern <- " , (*SKIP)(*FAIL)|,"
data <- c("aaa,aaa", "bbb, bbb", "ddd , ddd")
strsplit(data, pattern, perl = T)

This yields the same as above.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174874

regular expression has to consider that no whitespace should occur after the comma

Use negative lookahead assertion:

> strsplit(c("aaa,aaa", "bbb, bbb", "ddd , ddd"), ",(?!\\s)", perl = TRUE)
[[1]]
[1] "aaa" "aaa"

[[2]]
[1] "bbb, bbb"

[[3]]
[1] "ddd , ddd"

,(?!\\s) matches , only if it's not followed by a space

Upvotes: 5

Related Questions