Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby regex find and replace

Tags:

regex

ruby

I have the following output:
time = 15:40:32.81

And I want to eliminate : and the . so that it looks like this:
15403281

I tried doing a

time.gsub(/\:\s/,'')

but that didn't work.

like image 640
rahrahruby Avatar asked Aug 18 '10 18:08

rahrahruby


People also ask

How do I replace in Ruby?

The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Parameters: The function accepts an enumerable object which to be replaced in the set.

How do you replace a character in a string in Ruby?

Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string.

How do I use GSUB in Ruby?

gsub! is a String class method in Ruby which is used to return a copy of the given string with all occurrences of pattern substituted for the second argument. If no substitutions were performed, then it will return nil. If no block and no replacement is given, an enumerator is returned instead.

What is sub in Ruby?

The sub() method replaces just the first instance of a string with another. Gsub meanwhile replaces all instances. Thus:Gsub is closest to a “replace string” method. Sub() is conceptually a “replace first string” method. Ruby program that compares sub, gsubvalue = "abc abc"


1 Answers

"15:40:32.81".gsub(/:|\./, "")
like image 192
Jed Schneider Avatar answered Oct 15 '22 23:10

Jed Schneider