# Parse types from string

**URL:** <https://discourse.julialang.org/t/parse-types-from-string/84518>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 20, 2022, 8:18am UTC](https://discourse.julialang.org/t/parse-types-from-string/84518 "2022-07-20T08:18:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 20, 2022, 8:18am UTC](https://discourse.julialang.org/t/parse-types-from-string/84518/1 "2022-07-20T08:18:34Z")

</div>

If I have a string of a function how can I parse the types from it?

```julia
str = """
foo(
    bar::Bar, baz::AbstractVector{Baz}, 
    var::Dict{Symbol,Var}=d, zar::Dict{Symbol,Zar} = k,
    rar::Dict{Symbol,Rar},
	rar::Dict{Symbol,Rar}
)
"""

```

With this one the match is until the first colon it finds, which is incorrect for the `Dict{Symbol,Type}`  
`r"::(.+?)[,| =|=]"`

Is there a package that already does this?

---

<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:** [July 20, 2022, 8:32am UTC](https://discourse.julialang.org/t/parse-types-from-string/84518/2 "2022-07-20T08:32:45Z")

</div>

You can use Julia’s parser `ex = Meta.parse(str)` and then inspect the returned expression `ex`.

* * *

Why do you have a string like this in the first place though? Since this is not something you usually need to to when writing Julia code, it sounds a bit like an [XY](https://xyproblem.info/) problem, and perhaps there is a better way to solve the actual problem.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 20, 2022, 11:12am UTC](https://discourse.julialang.org/t/parse-types-from-string/84518/3 "2022-07-20T11:12:03Z")

</div>

It is for generating a doc string:

> <https://github.com/baggepinnen/AutomaticDocstrings.jl/blob/ba2e35e0325bc0708c19dd07b992bc0257e9f030/src/AutomaticDocstrings.jl#L112>

and optionally removing the arg types from the doc string.
