Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is some example code which uses regex to find all the text between <code>&lt;body&gt;...&lt;/body&gt;</code> tags. Although this demonstrates some features of python's re module, note that the <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html" rel="nofollow noreferrer">Beautiful Soup</a> module is very easy to use and is a better tool to use if you plan on parsing HTML or XML. (See below for an example of how you could parse this using BeautifulSoup.)</p> <pre><code>#!/usr/bin/env python import re # Here we have a string with a multiline &lt;body&gt;...&lt;/body&gt; fileString='''baz&lt;body&gt;foo baby foo baby foo baby foo &lt;/body&gt;&lt;body&gt;bar&lt;/body&gt;''' # re.DOTALL tells re that '.' should match any character, including newlines. x = re.search('(&lt;body&gt;.*?&lt;/body&gt;)', fileString, re.DOTALL) for match in x.groups(): print(match) # &lt;body&gt;foo # baby foo # baby foo # baby foo # &lt;/body&gt; </code></pre> <p>If you wish to collect all matches, you could use re.findall:</p> <pre><code>print(re.findall('(&lt;body&gt;.*?&lt;/body&gt;)', fileString, re.DOTALL)) # ['&lt;body&gt;foo\nbaby foo\nbaby foo\nbaby foo\n&lt;/body&gt;', '&lt;body&gt;bar&lt;/body&gt;'] </code></pre> <p>and if you plan to use this pattern more than once, you can pre-compile it:</p> <pre><code>pat=re.compile('(&lt;body&gt;.*?&lt;/body&gt;)', re.DOTALL) print(pat.findall(fileString)) # ['&lt;body&gt;foo\nbaby foo\nbaby foo\nbaby foo\n&lt;/body&gt;', '&lt;body&gt;bar&lt;/body&gt;'] </code></pre> <p>And here is how you could do it with BeautifulSoup:</p> <pre><code>#!/usr/bin/env python from BeautifulSoup import BeautifulSoup fileString='''baz&lt;body&gt;foo baby foo baby foo baby foo &lt;/body&gt;&lt;body&gt;bar&lt;/body&gt;''' soup = BeautifulSoup(fileString) print(soup.body) # &lt;body&gt;foo # baby foo # baby foo # baby foo # &lt;/body&gt; print(soup.findAll('body')) # [&lt;body&gt;foo # baby foo # baby foo # baby foo # &lt;/body&gt;, &lt;body&gt;bar&lt;/body&gt;] </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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