Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are searching a list of URLs </p> <pre><code>urls = [ 'http://some.link.com/path/to/file.jpg', 'http://some.link.com/path/to/another.png', 'http://and.another.place.com/path/to/not-image.txt', ] </code></pre> <p>to find ones that match a given pattern you can use:</p> <pre><code>import re for url in urls: if re.match(r'http://.*(jpg|png|gif)$'): print url </code></pre> <p>which will output</p> <pre class="lang-none prettyprint-override"><code>http://some.link.com/path/to/file.jpg http://some.link.com/path/to/another.png </code></pre> <p><code>re.match()</code> will test for a match at the start of the string and return a <em>match object</em> for the first two links, and <code>None</code> for the third.</p> <p>If you are getting just the extension, you can use the following:</p> <pre><code>for url in urls: m = re.match(r'http://.*(jpg|png|gif)$') print m.group(0) </code></pre> <p>which will print</p> <pre class="lang-none prettyprint-override"><code>('jpg',) ('png',) </code></pre> <p>You will get just the extensions because that's what was defined as a group.</p> <p>If you need to find the url in a long string of text (such as returned from wget), you need to use <code>re.search()</code> and enclose the part you are interested in with <code>( )</code>'s. For example, </p> <pre><code>response = """dlkjkd dkjfadlfjkd fkdfl kadfjlkadfald ljkdskdfkl adfdf kjakldjflkhttp://some.url.com/path/to/file.jpgkaksdj fkdjakjflakdjfad;kadj af kdlfjd dkkf aldfkaklfakldfkja df""" reg = re.search(r'(http:.*/(.*\.(jpg|png|gif)))', response) print reg.groups() </code></pre> <p>will print</p> <pre class="lang-none prettyprint-override"><code>('http://some.url.com/path/to/file.jpg', 'file.jpg', 'jpg',) </code></pre> <p>or you can use <code>re.findall</code> or <code>re.finditer</code> in place of <code>re.search</code> to get all of the URL's in the long response. Search will only return the first one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload