Parse types from string

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

str = """
    bar::Bar, baz::AbstractVector{Baz}, 
    var::Dict{Symbol,Var}=d, zar::Dict{Symbol,Zar} = k,

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?

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 problem, and perhaps there is a better way to solve the actual problem.

1 Like

It is for generating a doc string:

and optionally removing the arg types from the doc string.