I have a character vector.
x <- c('This is a simple text', 'this is a veryyyyyyyyyy long word', 'Replacethis andalsothis')
I want to insert a space between a word which is longer than n
characters. For this example, we can consider n = 10
. I would prefer a regex
solution but if you think there are other alternatives I wouldn't mind to try that.
The output that I am looking for -
c('This is a simple text', 'this is a veryyyyyyy yyy long word', 'Replacethi s andalsothi s')
I have tried using the solution from this post by making the necessary changes for my data but it doesn't give the required output.
sub('(.{10})(?=\\S)//g', '\\1 ', x, perl = TRUE)
#[1] "This is a simple text" "this is a veryyyyyyyy long word" "Replacethis andalsothis"
CodePudding user response:
You can use
gsub("\\b(\\w{10})\\B", "\\1 ", x) # If your words only consist of letters/digits/_
gsub("(?<!\\S)(\\S{10})(?=\\S)", "\\1 ", x, perl=TRUE) # If the "words" are non-whitespace char chunks
See the regex demo and this regex demo, and the R demo:
x <- c('This is a simple text', 'this is a veryyyyyyyyyy long word', 'Replacethis andalsothis')
gsub("\\b(\\w{10})\\B", "\\1 ", x)
# => [1] "This is a simple text" "this is a veryyyyyyy yyy long word" "Replacethi s andalsothi s"
x <- c("this is a veryyyyyyy|yyy long word")
gsub("(?<!\\S)(\\S{10})(?=\\S)", "\\1 ", x, perl=TRUE)
# => [1] "this is a veryyyyyyy |yyy long word"
The regex matches...
\b
- a word boundary(\w{10})
- ten word chars\B
- only when there is another word char appears immediately on the right (so, the tenth word char is not the end char of the word).
And
(?<!\S)
- a location at the start of string or right after a whitespace(\S{10})
- Group 1: ten non-whitespace chars(?=\S)
- immediately to the right, there must be a non-whitespace char.