Python regex : trimming special characters
<p>Is it possible to remove special characters using regex?</p>

<p>I'm attempting to trim:</p>

<pre><code>\n\t\t\t\t\t\t\t\t\t\tButte County High School\t\t\t\t\t\t\t\t\t
</code></pre>

<p>down to:</p>

<pre><code>Butte County High School
</code></pre>

<p>using    </p>

<pre><code>regexform = re.sub("[A-Z]+[a-z]+\s*",'', schoolstring)
print regexform
</code></pre>

<p>If you're really set on using regular expressions:</p>

<pre><code>re.sub(r'^\s+|\s+$', '', schoolstring)
</code></pre>

<p>This will work for:</p>

<pre><code>'   this is a test   '   # multiple leading and trailing spaces
' this is a test '       # one leading and trailing space
'this is a test'         # no leading or trailing spaces
'\t\tthis is a test\t\t' # leading or trailing whitespace characters
</code></pre>

<p>This expression one or more whitespace characters from the start <code>^\s+</code> of the string or <code>|</code> one or more whitespace characters from the end of the string <code>\s+$</code>.</p>

<p>However, <code>string.strip()</code> is simpler for removing leading and trailing space.</p>

python regex expression