Create new regex from previously existing regex

I have a regular expression to match integers just like this
type_int = r"\s*[0-9]*$"

And I would like to create a regular expression to match by using my type_int definition inside the type_float definition on something like this

type_float = r"(^\s*)(-|+)?({type_int})(.)({type_int})$"

an optional ‘+’ or ‘-’ signal, followed by an integer, then a dot ‘.’, then another integer

But it’s not working. Is there a way to use my {type_int} name inside the new definition?

Thanks

You can perhaps use the Regex constructor.

I recall scratching my head about this. I rarely (if ever?) see examples of using the constructor.

julia> r1 = r"^cat"
r"^cat"

julia> animal = "cat";

julia> r2 = Regex("^$animal")
r"^cat"

julia> r1 == r2
true

julia> r1.pattern
"^cat"

julia> typeof(r1.pattern)
String

Would https://github.com/JuliaLang/julia/pull/23422 work for you? Maybe a good time to finish this PR…
EDIT: I realize I should have given an example, which would be I think in your case something like:
type_float = "(^\s*)(-|+)?(" * type_int * ")(.)(" * type_int * ")$"

1 Like

Funny, I use it all the time! Like op, I often match a regular expression and then use the match to build a new one.

Here is a possible solution.
First define a non-standard string macro

macro rs_str(pattern)
    :(Regex(replace($pattern, r"\{\w+\}", x->eval(Symbol(x[2:end-1])).pattern)))
end

Then

julia> type_int = r"\s*[0-9]*$"
 r"\s*[0-9]*$"

# non-standard string rs"..." produces a new regular expression
julia> rs"(^\s*)(-|\+)?({type_int})(.)({type_int})$" 
r"(^\s*)(-|\+)?(\s*[0-9]*$)(.)(\s*[0-9]*$)$"

Note that your example is not very suitable to identify floating point numbers. But that is beside the point of this exercise :wink: