Retrieving log message within curly braces using Regex with Python
<p>I am trying to parse some logs which return some responses in a key-pair format. I only want they value contained by the last key-pair (Rs: {".."}). The information I want are enclosed inside the curly braces.</p>

<p>What I have done is to use regex to match anything inside the curly braces like this:</p>

<pre><code>import re

log = '2016-10-13 17:04:50 - info - uri:"GET x/y/z" ip:1.1.1.1 Rs:{"data": "blah blah"}'

text = re.compile("Rs\:{(.*)\}").search(log).group(1)
print (text)

&gt;&gt;&gt; "data": "blah blah"

# Desired results
&gt;&gt;&gt; {"data": "blah blah"}
</code></pre>

<p>However there are some issues doing it this way:</p>

<ol>
<li><p>I also wanted the starting curly braces and closing curly braces.</p></li>
<li><p>This method doesn't work if there other opening ("{") or closing ("}:) curly braces before or inside the Rs values.</p></li>
</ol>

<p>Is there a better way to do this?</p>

<p>The first part is easy: just move the capturing parens out a little bit use this as your regex:</p>

<pre><code>"Rs:(\{.*\})"
</code></pre>

<p>The other problem is more complicated - if you want the rest of the line (starting at <code>{</code>), then </p>

<pre><code>r'Rs:(\{.*)\Z'
</code></pre>

<p>would get you what you want.</p>

python regex