红宝石正则表达式找到并替换

rah*_*uby 7 ruby regex

我有以下输出:
time = 15:40:32.81

我想消除:.,使它看起来像这样:
15403281

我试过做一个

time.gsub(/\:\s/,'')
Run Code Online (Sandbox Code Playgroud)

但那没用.

Jed*_*der 12

"15:40:32.81".gsub(/:|\./, "")
Run Code Online (Sandbox Code Playgroud)


Dan*_*uis 5

time = '15:40:32.81'
numeric_time = time.gsub(/[^0-9]+/, '')
# numeric_time will be 15403281
Run Code Online (Sandbox Code Playgroud)

[^0-9] specifies a character class containing any character which is not a digit (^ at the beginning of a class negates it), which will then be replaced by an empty string (or, in other words, removed).

(Updated to replace \d with 0-9 for clarity, though they are equivalent).