I want to replace all but first consecutive dots. Here is an example of what I want:
> names.orig <- c("test & best", "test & worse &&&& ? do")
> names <- make.names(names.orig)
> names
[1] "test...best" "test...worse.........do"
>
> # But I want this instead:
> # [1] "test.best" "test.worse.do"
>
> # Desperatley tried:
> gsub("\\.{2, }", "", names)
[1] "testbest" "testworsedo"
> gsub("\\G((?!^).*?|[^\\.]*\\.*?)\\.", "", names)
Error in gsub("\\G((?!^).*?|[^\\.]*\\.*?)\\.", "", names) :
invalid regular expression '\G((?!^).*?|[^\.]*\.*?)\.', reason 'Invalid regexp'
> # etc.
>
> # The only thing that works for me is this
> unlist(lapply(strsplit(names, "\\."), function(x) paste(x[x != ""], collapse=".")))
[1] "test.best" "test.worse.do"
>
> # But, really, what is the right regex in combination with what?
How to solve this with regex?
Use the String. replace() method to remove all dots from a string, e.g. const dotsRemoved = str. replace(/\./g, ''); . The replace() method will remove all dots from the string by replacing them with empty strings.
Save this answer. Show activity on this post. def add_dots(to_add): res="" for letter in to_add: res= res + letter + "." print{:-1}res) def remove_dots(to_remove): print (to_remove): add_dots("test") remove_dots("t.e.s.t.")
The standard solution to remove punctuations from a String is using the replaceAll() method. It can remove each substring of the string that matches the given regular expression.
To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space.
Replace the ""
with "."
in your first regex:
R> nms <- make.names(c("test & best", "test & worse &&&& ? do"))
R> gsub("\\.{2, }", ".", nms)
[1] "test.best" "test.worse.do"
This also works. Basically, you're replacing all dots and consecutive dots with a single dot.
R> gsub("\\.+", ".", nms)
[1] "test.best" "test.worse.do"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With