Removing spaces after a specific ";"

I have a question about removing spaces inside character text inside a column data frame. This is my data frame column:

head(data$HO)
[1] "Lidar; Wind field; Temperature; Aerosol; Fabry-Perot etalon"                             
[2] "Compressive ghost imaging; Guided filter; Single-pixel imaging"    

This question is different from this link , because I want to remove only spaces after the ";" character, so the output should look like this:

head(data$HO)
[1] "Lidar;Wind field;Temperature;Aerosol;Fabry-Perot etalon"                             
[2] "Compressive ghost imaging;Guided filter;Single-pixel imaging"    

I tried

data$HO <- gsub("\\;s", ";",data$HO)

but that will not work.

Any suggestion?

+4
source share
2 answers

You can use the template ;\s+and replace it with ;:

> x <- c("Lidar; Wind field; Temperature; Aerosol; Fabry-Perot etalon", "Compressive ghost imaging; Guided filter; Single-pixel imaging")
> gsub(";\\s+", ";", x)
[1] "Lidar;Wind field;Temperature;Aerosol;Fabry-Perot etalon"     
[2] "Compressive ghost imaging;Guided filter;Single-pixel imaging"

Template Details:

  • ; - half-column
  • \s+ - one or more space characters.

See the regex demo .

A few more solutions:

gsub("(*UCP);\\K\\s+", "", x, perl=TRUE)
gsub(";[[:space:]]+", ";", x)
+5
source

look-behind ?<=. ; \s+ .

v <- c("Lidar; Wind field; Temperature; Aerosol; Fabry-Perot etalon", 
      "Compressive ghost imaging; Guided filter; Single-pixel imaging")

gsub("(?<=;)\\s+", "", v, perl = TRUE)

# Result:
# [1] "Lidar;Wind field;Temperature;Aerosol;Fabry-Perot etalon"     
# [2] "Compressive ghost imaging;Guided filter;Single-pixel imaging"
0

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


All Articles