title: Looking for an efficient way to remove all occurrences of a pattern in a string
body: <p>I have many text files (10,000,000) and I want to remove all tokens which are started with <em>/m/</em> from them. I wrote the following code but I am looking for faster code because I tokenize string then join tokens again. </p>
  
  <pre><code>import nltk
  def removeMId(str):
  words=[]
  tokens = nltk.word_tokenize(str)
  i=0
  for tkn in tokens:
      if not tkn.startswith('/m/'):
         words.append(tkn)
         i+=1
  return ' '.join(words)
  </code></pre>
  
  <p>All removal tokens follow this <code>pattern=r''' /\m+(?:[-'/]\w+)*'''</code></p>
  
  <p>This is an example of input:</p>
  
  <blockquote>
    <p>the manuscript handling fee (checks must be in <strong>/m/09c7w0</strong>  dollars, drawn on a <strong>/m/09c7w0</strong>  bank, MICR encoded at the bottom of the check, and payable to ASPET). <strong>/m/01kqjn</strong>  <strong>/m/021b7r</strong>  and <strong>/m/01w6dw</strong>  credit cards are also accepted.  </p>
  </blockquote>