# Create new regex from previously existing regex

**URL:** <https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156>\
**Category:** General Usage\
**Tags:** regex\
**Created:** [July 4, 2018, 12:22pm UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156 "2018-07-04T12:22:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naelson\_douglas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naelson_douglas/32/1790_2.png) [@Naelson\_douglas](https://discourse.julialang.org/u/Naelson_douglas)\
**Post date:** [July 4, 2018, 12:22pm UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/1 "2018-07-04T12:22:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 4, 2018, 12:31pm UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/2 "2018-07-04T12:31:56Z")

</div>

You can perhaps use the `Regex` constructor.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [July 4, 2018, 12:38pm UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/3 "2018-07-04T12:38:47Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [July 4, 2018, 1:36pm UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/4 "2018-07-04T13:36:50Z")

</div>

Would [https://github.com/JuliaLang/julia/pull/23422](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 * ")$"`

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 5, 2018, 12:25am UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/5 "2018-07-05T00:25:22Z")

</div>

> [@jlapeyre](#):
>
> I recall scratching my head about this. I rarely (if ever?) see examples of using the constructor.

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.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [July 5, 2018, 1:10am UTC](https://discourse.julialang.org/t/create-new-regex-from-previously-existing-regex/12156/6 "2018-07-05T01:10:16Z")

</div>

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

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

```

Then

```julia
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 😉
