Remove all separators at the beginning and end of a line

After I split the lines and separate them with a semicolon, I would like to remove the semicolons in front and back of my line. A few semicolons represent spaces in a cell. For example, after a crash, an observation might look like this:

;TX;PA;CA;;;;;;; 

I want the cell to look like this:

 TX;PA;CA 

Here is my collapse code:

 new_df <- group_by(old_df, unique_id) %>% summarize_each(funs(paste(., collapse = ';'))) 

If I try to use gsub for a semicolon, it removes all of them. If, if I remove the end character, it just removes one of the semicolons. Any ideas on how to remove everything at the beginning and end, but leaving those that are between the observations? Thanks.

+6
source share
2 answers

use regex ^;+|;+$

 x <- ";TX;PA;CA;;;;;;;" gsub("^;+|;+$", "", x) 

^ indicates the beginning of a line, + indicates several matches, and $ indicates the end of a line. | indicates "OR". So, combined, he searches for any number ; at the beginning of a line OR any number ; at the end of the line and replaces them with blank space.

+10
source

The stringi package allows stringi to specify the patterns that you want to save, and trim everything else. If you only have letters (although you can specify a different pattern), you can just do

 stringi::stri_trim_both(";TX;PA;CA;;;;;;;", "\\p{L}") ## [1] "TX;PA;CA" 
+3
source

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


All Articles