Remove spaces between characters more

I have a line like this:

foo > bar > foo bar > foo > test > test this

I would like to take any lines within a range other than characters, and convert them into separate words without spaces between them, but save all other spaces as follows:

foo > bar > foobar > foo > test > testthis

I tried using gsub to remove spaces gsub(" ", "", x, fixed = TRUE), but I'm not sure how to do this only within characters more than when saving spaces next to characters more

+4
source share
3 answers

PCRE SKIP/FAIL (\\s*), >, (\\s*). (*SKIP) . (*FAIL) (* SKIP), (|\\s+) ((*FAIL)) ("")

gsub("\\s*\\>\\s*(*SKIP)(*FAIL)|\\s+", "", str1, perl = TRUE)
#[1] "foo > bar > foobar > foo > test > testthis"

- . regex lookbehind ((?<=\\w)) ((?=\\w|\\$))

gsub("(?<=\\w)\\s(?=\\w|\\$)", "", str1, perl = TRUE)
#[1] "foo > bar > foobar > foo > test > testthis"

,

gsub("(\\w)\\s(\\w)", "\\1\\2", str1)
#[1] "foo > bar > foobar > foo > test > testthis"

str1 <- "foo > bar > foo bar > foo > test > test this"
+4

, , , >, ( (\s*>\s*)), , 1+ (\s+) - , 1 (\1):

gsub("(\\s*>\\s*)|\\s+", "\\1", x)

, Unicode,

gsub("(*UCP)(\\s*>\\s*)|\\s+", "\\1", x, perl=TRUE)

regex.

  • (\s*>\s*) - 1: 0+, >, 0+
  • | -
  • \s+ - 1 + .

R demo online:

x <- "foo > bar > foo bar > foo > test > test this"
gsub("(\\s*>\\s*)|\\s+", "\\1", x)
## => [1] "foo > bar > foobar > foo > test > testthis"
+1

Here is a (possibly) more acceptable solution for non-regex-expert:

# Split into parts
str2 <- unlist(strsplit(str1, ">"))
str2
[1] "foo "       " bar "      " foo bar "  " foo "      " test "     " test this"

# Eliminate all spaces
str3 <- gsub(" ", "", str2)
str3
[1] "foo"      "bar"      "foobar"   "foo"      "test"     "testthis"

# And now paste again together 
str_final <- paste(str3, collapse = " > ")
str_final
[1] "foo > bar > foobar > foo > test > testthis"
0
source

Source: https://habr.com/ru/post/1692903/


All Articles