# How get only numbers from a string

**URL:** <https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524>\
**Category:** General Usage\
**Created:** [May 20, 2021, 3:47pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524 "2021-05-20T15:47:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Brus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_brus/32/23767_2.png) [@Rafael\_Brus](https://discourse.julialang.org/u/Rafael_Brus)\
**Post date:** [May 20, 2021, 3:47pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/1 "2021-05-20T15:47:21Z")

</div>

I need get only numbers caracters from string

```julia
a = ["00023'4", "@00452a", "456ad52"]

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 20, 2021, 3:51pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/2 "2021-05-20T15:51:34Z")

</div>

Here’s one option:

```julia
julia> [filter(isdigit, collect(s)) for s in a]
3-element Vector{Vector{Char}}:
 ['0', '0', '0', '2', '3', '4']
 ['0', '0', '4', '5', '2']
 ['4', '5', '6', '5', '2']

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [May 20, 2021, 3:51pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/3 "2021-05-20T15:51:45Z")

</div>

Something like this?

```julia
julia> filter(x->'0'<=x<='9',"0123456789@3434341@!\$#%%^%\$#2343240")
"012345678934343412343240"

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [May 20, 2021, 4:08pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/4 "2021-05-20T16:08:35Z")

</div>

@sijo has better altenative for digit checking by `filter(isdigit, "0123456789@3434341@!\$#%%^%\$#2343240")`.

---

<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:** [May 20, 2021, 4:09pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/5 "2021-05-20T16:09:35Z")

</div>

> [@sijo](#):
>
> `[filter(isdigit, collect(s)) for s in a]`

Never use `collect`:

```julia
jl> [filter(isdigit, s) for s in a]
3-element Vector{String}:
 "000234"
 "00452"
 "45652"

```

or

```julia
jl> filter.(isdigit, a)
3-element Vector{String}:
 "000234"
 "00452"
 "45652"

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 20, 2021, 4:17pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/6 "2021-05-20T16:17:02Z")

</div>

> [@DNF](#):
>
> Never use `collect` :

unless you actually want an array of characters 🙂

---

<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:** [May 20, 2021, 4:26pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/7 "2021-05-20T16:26:24Z")

</div>

It’s not completely clear what you want, so here are a couple of alternatives:

```julia
jl> a = ["00023'4", "@00452a", "456ad52"];

jl> [[m.match for m in eachmatch(r"\d+", str)] for str in a]
3-element Vector{Vector{SubString{String}}}:
 ["00023", "4"]
 ["00452"]
 ["456", "52"]

jl> [[parse(Int, m.match) for m in eachmatch(r"\d+", str)] for str in a]
3-element Vector{Vector{Int64}}:
 [23, 4]
 [452]
 [456, 52]

```

(BTW, I’m not very happy about the interface to regex matches, where you have to access the internal fields of the object, like it was _Python_, or something. Why aren’t there any methods for accessing the matched strings? This always seems un-idiomatic to me.)

---

<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:** [May 20, 2021, 4:30pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/8 "2021-05-20T16:30:46Z")

</div>

> [@sijo](#):
>
> unless you actually want an array of characters 🙂

Hmmmfff. Well, if you _must_, this is faster:

```julia
 filter!.(isdigit, collect.(a))

```

😉

---

<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 12, 2022, 2:44pm UTC](https://discourse.julialang.org/t/how-get-only-numbers-from-a-string/61524/9 "2022-10-12T14:44:36Z")

</div>

and if `a` contains real numbers you might use regular expressions:  
[parse by means of regular expression](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126/8)
