# Regex in replacements: how to use matches

**URL:** <https://discourse.mitmproxy.org/t/regex-in-replacements-how-to-use-matches/1429>\
**Category:** help\
**Created:** [February 20, 2019, 8:15am UTC](https://discourse.mitmproxy.org/t/regex-in-replacements-how-to-use-matches/1429 "2019-02-20T08:15:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercuriev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mercuriev/32/398_2.png) [@mercuriev](https://discourse.mitmproxy.org/u/mercuriev)\
**Post date:** [February 20, 2019, 8:15am UTC](https://discourse.mitmproxy.org/t/regex-in-replacements-how-to-use-matches/1429/1 "2019-02-20T08:15:30Z")

</div>

Hello all.

I want to inject a HTML tag after original `<body>` and I’m using `replacements` option for it:

```auto
replacements:
        - "#~s#<body(.*?)>#<body$1><iframe src='http://localhost:8000'></iframe>"

```

How can I use matched groups in regex so that I can replay original `body` attributes?  
Or is there a better way?

---

<div class="post-metadata">

**Author:** ![mercuriev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mercuriev/32/398_2.png) [@mercuriev](https://discourse.mitmproxy.org/u/mercuriev)\
**Post date:** [February 20, 2019, 10:27am UTC](https://discourse.mitmproxy.org/t/regex-in-replacements-how-to-use-matches/1429/2 "2019-02-20T10:27:34Z")

</div>

I found this by trial and error:

```auto
#~s#<body(.*?)>#<body\\g<1>><iframe src='http://localhost:8000'></iframe>

```

Is that `\\g<1>` syntax simplest? I’m not python fluent. Is there a way to write it simpler?

---

<div class="post-metadata">

**Author:** ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)\
**Post date:** [February 25, 2019, 7:47am UTC](https://discourse.mitmproxy.org/t/regex-in-replacements-how-to-use-matches/1429/3 "2019-02-25T07:47:02Z")

</div>

I usually help myself testing regular expression on [this](https://regex101.com/) site.
