我有以下输出:
time = 15:40:32.81
我想消除:和.,使它看起来像这样:
15403281
我试过做一个
time.gsub(/\:\s/,'')
Run Code Online (Sandbox Code Playgroud)
但那没用.
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).
| 归档时间: |
|
| 查看次数: |
8437 次 |
| 最近记录: |