Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all dots but the first in a character string

Tags:

regex

r

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?

like image 732
Samo Avatar asked Jun 03 '13 01:06

Samo


People also ask

How do I remove all dots from a string?

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.

How do you remove the dots from a string in Python?

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.")

How do you cut a string out of a dot in Java?

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.

How do I remove the dots from numbers in R?

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.


1 Answers

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"
like image 80
Joshua Ulrich Avatar answered Nov 14 '22 23:11

Joshua Ulrich