How to get "clean" match results in Python
<p>I am a total noob, coding for the first time and trying to learn by doing. 
I'm using this:</p>

<pre><code>import re
f = open('aaa.txt', 'r')
string=f.read()
c = re.findall(r"Guest last name: (.*)", string)
print "Dear Mr.", c
</code></pre>

<p>that returns </p>

<pre><code>Dear Mr. ['XXXX']
</code></pre>

<p>I was wondering, is there any way to get the result like</p>

<pre><code>Dear Mr. XXXX
</code></pre>

<p>instead? </p>

<p>Thanks in advance.</p>

<p>Yes use <code>re.search</code> if you only expect one match:</p>

<pre><code>re.search(r"Guest last name: (.*)", string).group(1)`
</code></pre>

<p>findall is if you expect multiple matches. You probably want to also add <code>?</code> to your regex <code>(.*?)</code> for a non-greedy capture but you also probably want to be a little more specific and capture up to the next possible character after the name/phrase you want.</p>

python regex python-2.7 match