Extracting a float from a string

Any particular reason you want to avoid regexps? They are often a good solution for tasks like this.

To compare with @oheil’s nice solution,

using BenchmarkTools
strg="time t=0.234_ext.bson"
f1(str) = parse(Float64, split(split(str, '=')[2], '_')[1])
f2(str) = parse(Float64, match(r".*=(.*)_.*", str).captures[1])

julia> @btime f1($strg)
  592.303 ns (6 allocations: 448 bytes)
0.234

julia> @btime f2($strg)
  389.634 ns (4 allocations: 288 bytes)
0.234

In addition, it is easier to do more validation wiht regexps.

6 Likes