# Extracting a float from a string

**URL:** https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126
**Category:** New to Julia
**Tags:** strings, regex
**Created:** [July 15, 2020, 12:45pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126 "2020-07-15T12:45:01Z")
**Posts on this page:** 12
**Page:** 2

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 13, 2022, 1:35pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/22 "2022-10-13T13:35:52Z")

</div>

If you don’t know which number format will be matched, you cannot specify the number type to `parse` anyway.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [October 13, 2022, 1:37pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/23 "2022-10-13T13:37:55Z")

</div>

Beware, that’s not type-stable, you may want to do:

```julia
Float32(Meta.parse("-1.234f0"))

```

I was thinking why doesn’t parse(Float32, “-1.234f04”) call this Meta.parse I didn’t of (or Meta; and why do we want to know about Meta/use that), but the former is type-stable, and the latter isn’t, it depends on the string passed in (can also e.g. parse strings, but then as `Symbol`s). I think it’s intentional that the regular parse doesn’t support “f0” ending, you may not want it if files, unless maybe when you do…

One other difference, in case it matters to you:

```julia
:Inf

julia> parse(Float32, "Inf")
Inf32

```

Same issue with parsing NaN, plus:

```julia
julia> Meta.parse("-NaN") # is Expr, unlike Symbol without the minus:
:(-NaN)

```

[I didn’t know -NaN is a distinct bit-pattern in IEEE floats until recently, but I think you may never encounter it or be able to parse it, anyone know if it CAN pop up?]

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 13, 2022, 1:41pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/24 "2022-10-13T13:41:08Z")

</div>

> [@Palli](#):
>
> that’s not type-stable

But with that regex, we don’t know what the float type will be anyway.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [October 13, 2022, 1:55pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/25 "2022-10-13T13:55:51Z")

</div>

I mean if you want it type-unstable that’s ok (and I like type-unstable code allowed, Julia more powerful that way), it’s just often you don’t want that. If the point is to just get the value, in a type-stable way, then I guess `Float64(Meta.parse("-1.234f0"))` is usually better, to not round to lower precision Float32 when you actually get a Float64.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 13, 2022, 2:26pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/26 "2022-10-13T14:26:06Z")

</div>

> [@Palli](#):
>
> `Float64(Meta.parse("-1.234f0"))`

Some how as a kind of a summary, my proposal:

```julia
extract_number(_str) = Float64(Meta.parse(match(r"-?\d*\.?\d+([ef][+-]?\d*)?", _str).match))

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [October 18, 2022, 5:38pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/27 "2022-10-18T17:38:47Z")

</div>

I don’t know how general and efficient it is, but maybe it can give the OP,since he doesn’t want to use regex, a useful hint

```julia

strg="time t=0.234_ext.bson"
r=findlast(isdigit, strg)
l=findfirst(isdigit, strg)
sgn=(strg[l-1]=='-')*1
strg[l-sgn:r]

strg="time t=0.234f3_ext.bson"
r=findlast(isdigit, strg)
l=findfirst(isdigit, strg)
sgn=strg[l-1]=='-'
strg[l-sgn:r]

```

another way without using regex (you can add the handling of any ‘-’ sign)

```julia
idx=filter(c-> xor(isdigit(strg[c-1]), isdigit(strg[c+1])), 2:length(strg)-1 )

strg[range(extrema(idx).+(1,-1)...)]

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 24, 2022, 11:54am UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/28 "2022-10-24T11:54:57Z")

</div>

Unfortunatly `Float64(Meta.parse("Inf"))` does not work ☹  
Any ideas?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 24, 2022, 12:00pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/29 "2022-10-24T12:00:50Z")

</div>

> [@ellocco](#):
>
> parse cannot handle “f+/-xx”

Use the [Parsers.jl package](https://github.com/JuliaData/Parsers.jl):

```julia
julia> Parsers.parse(Float64, "-1.234f-4")
-0.0001234

```

See also the discussion in [julia#44910](https://github.com/JuliaLang/julia/pull/44910) and [julia#5690](https://github.com/JuliaLang/julia/issues/5690).

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 24, 2022, 12:01pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/30 "2022-10-24T12:01:07Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> idx=filter(c-> xor(isdigit(strg[c-1]), isdigit(strg[c+1])), 2:length(strg)-1 )
> 
> strg[range(extrema(idx).+(1,-1)...)]
> 
> ```

I do not understand your code snippet above.

If I define:

```julia
strg = "2.968e-01"
idx=filter(c-> xor(isdigit(strg[c-1]), isdigit(strg[c+1])), 2:length(strg)-1 )
strg[range(extrema(idx).+(1,-1)...)]

```

The error is:

```julia
ERROR: ArgumentError: At least one of `length` or `step` must be specified

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 24, 2022, 12:07pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/31 "2022-10-24T12:07:35Z")

</div>

Proposal v2:

```julia
extract_number(_str) = Parsers.parse(Float64, match(r"-?(Inf|(\d*\.?\d+([ef][+-]?\d*)?))", _str).match)

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [October 24, 2022, 1:06pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/32 "2022-10-24T13:06:36Z")

</div>

The error seems related to the function `range(...)`. Unfortunatelly I can’t reproduce.  
I take this opportunity to correct and make the snippet more generally valid

```julia
julia> strg = "xxx2.968e-01xxx"
"xxx2.968e-01xxx"

julia> idx=filter(c-> xor(isdigit(strg[max(1,c-1)]), isdigit(strg[min(length(strg),c+1)])), 1:length(strg) )
8-element Vector{Int64}:
  3
  6
  8
  9
 10
 11
 12
 13

julia> strg[range(extrema(idx).+(1,-1)...)]
"2.968e-01"

```

PS  
Although it is not the most suitable solution for the case in question, I just wanted to illustrate the use of the XOR function that I have seen usefully used in another context

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 24, 2022, 1:10pm UTC](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/33 "2022-10-24T13:10:53Z")

</div>

> [@rocco\_sprmnt21](#):
>
> The error seems related to the function `range(...)`. Unfortunatelly I can’t reproduce.

It is a Julia version issue, on Julia v1.8.2 it works for me.

[Previous page](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126.md?page=1)
