# String matching with variable interpolation

**URL:** <https://discourse.julialang.org/t/string-matching-with-variable-interpolation/62276>\
**Category:** New to Julia\
**Created:** [June 2, 2021, 1:14pm UTC](https://discourse.julialang.org/t/string-matching-with-variable-interpolation/62276 "2021-06-02T13:14:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [June 2, 2021, 1:14pm UTC](https://discourse.julialang.org/t/string-matching-with-variable-interpolation/62276/1 "2021-06-02T13:14:25Z")

</div>

Hi,

I have the following:

```julia
field = "out"
match(r"<out>\s*(.*?)\s*</out>",<out>hello</out>)

```

But I would like to replace the “out” in `<out>\s*(.*?)\s*</out>` by `field` to make it dynamic.  
How can i do that?

Thank you

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 2, 2021, 1:20pm UTC](https://discourse.julialang.org/t/string-matching-with-variable-interpolation/62276/2 "2021-06-02T13:20:32Z")

</div>

In general you should not use Regex to handle HTML (XML) since [Regex cannot parse HTML](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags).

for this specif use:

```julia
julia> field = "out"
"out"

julia> rg = "<$field>\\s*(.*?)\\s*</$field>"
"<out>\\s*(.*?)\\s*</out>"

julia> match(Regex(rg), "<out>hello</out>")
RegexMatch("<out>hello</out>", 1="hello")

```

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [June 2, 2021, 1:24pm UTC](https://discourse.julialang.org/t/string-matching-with-variable-interpolation/62276/3 "2021-06-02T13:24:29Z")

</div>

Thank you. The XML that I am trying to parse is in a weird format. I tried to use LightXML and EzXML but for this specific xml file it is not working.
