title: Django - replace parts of urlfield
body: <p>I can now load single video URLs of youtube. But the problem now is to load the playlist videos of youtube. So my question is, how do I replace two same pattern, but both having different replacements of a url?</p>
  
  <p>Eg:</p>
  
  <p>Actual url:</p>
  
  <pre><code>&lt;iframe width="400" height="327" src="http://www.youtube.com/embed/1UiICgvrsFI&amp;amp;list=PLvAOICvbvsbnc5dLG0YR9Mq_tFfzAhQSp&amp;amp;index=1" allowfullscreen="true"&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>Replace the pattern to become like this:</p>
  
  <pre><code>&lt;iframe width="560" height="315" src="//www.youtube.com/embed/1UiICgvrsFI?list=PLvAOICvbvsbnc5dLG0YR9Mq_tFfzAhQSp" allowfullscreen&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>Here the first <code>&amp;amp;</code> changes to <code>?</code> and the second <code>&amp;amp;</code> and its following contents i.e. <code>&amp;amp;index=1</code> are stripped.</p>
  
  <p>This is the models.py:</p>
  
  <pre><code>class Video(models.Model):
      title = models.CharField(max_length=100)
      video_url = models.URLField(max_length=100)
  
      def save(self, *args, **kwargs):
          new_url = (self.video_url.replace("watch?v=","v/"))
          super(Video, self).save(*args, **kwargs)
          if new_url:
              self.video_url = new_url
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>def save(self, *args, **kwargs):
          new_url = re.sub('watch\?v=','embed/',self.video_url)
          new_url = re.sub(r'^(http:\/\/)([\w\W]+)\&amp;amp;list=([\w\W]+)(\&amp;amp;index=[\d]+)$', r'//\2?list=\3', new_url)
          if new_url:
              self.video_url = new_url
              super(Video, self).save(*args, **kwargs)
  </code></pre>