# How to convert range (given as string) to true vector

**URL:** https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536
**Category:** New to Julia
**Created:** [December 11, 2022, 10:32pm UTC](https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536 "2022-12-11T22:32:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)
#### Post date: [December 11, 2022, 10:32pm UTC](https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536/1 "2022-12-11T22:32:16Z")

</div>

Hi to you all,

suppose I have given the string expression “1-5”. I would like to convert this to a true integer vector, i.e. [1, 2, 3, 4, 5]. That seems to be a bit tricky to me. Could someone give me a hint ?

Thanks

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 11, 2022, 11:14pm UTC](https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536/2 "2022-12-11T23:14:01Z")

</div>

Here is one way, using a [regular expression](https://docs.julialang.org/en/v1/manual/strings/#man-regex-literals), [`parse`](https://docs.julialang.org/en/v1/base/numbers/#Base.parse) and [`collect`](https://docs.julialang.org/en/v1/base/collections/#Base.collect-Tuple%7BAny%7D):

```julia
julia> str = "1-5";

julia> function range_str_to_vec(str)
           m = match(r"(\d+)\-(\d+)", str)
           m === nothing && error("good error message")
           r = parse(Int, m[1]) : parse(Int, m[2])
           collect(r)
       end;

julia> range_str_to_vec(str)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

Depending on your usecase you might be able to skip the collect since a range behaves more or less like a regular vector, but saves memory.

---

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [December 12, 2022, 2:00am UTC](https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536/3 "2022-12-12T02:00:31Z")

</div>

If you are not familiar to regular expression and the form will be not broken, then `split` function may be an easier way.

```julia
julia> input = "1-5"
"1-5"

julia> (a,b) = parse.(Int64, (split(input, '-')))
2-element Vector{Int64}:
 1
 5

julia> collect(a:b)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

### Author: ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)
#### Post date: [December 12, 2022, 5:36am UTC](https://discourse.julialang.org/t/how-to-convert-range-given-as-string-to-true-vector/91536/4 "2022-12-12T05:36:41Z")

</div>

Both solutions are great. Thank you very much!
