Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I extract a <p> containing <br> elements with a regex?
    primarykey
    data
    text
    <p>Let me start off by showing the 3 different type of strings I will be dealing with:</p> <pre><code>"&lt;h1&gt;Money Shake&lt;/h1&gt;&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;&lt;p&gt;Take money and stuff in blender.&lt;/p&gt;&lt;p&gt;Blend.&lt;/p&gt;" "&lt;h1&gt;Money Shake&lt;/h1&gt;&lt;p&gt;Posted by Gordon Gekko&lt;/p&gt;&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;&lt;p&gt;Take money and stuff in blender.&lt;/p&gt;&lt;p&gt;Blend.&lt;/p&gt;" "&lt;h1&gt;Money Shake&lt;/h1&gt;&lt;p&gt;Posted by Gordon Gekko&lt;/p&gt;&lt;p&gt;They're great&lt;/p&gt;&lt;p&gt;Yield: KA-CHING&lt;/p&gt;&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;&lt;p&gt;Take money and stuff in blender.&lt;/p&gt;&lt;p&gt;Blend.&lt;/p&gt;" </code></pre> <p>Essentially, what I wish to do is to rip out the chunk that has the ingredients:</p> <pre><code>"&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;" </code></pre> <p>This is the regex that I am using:</p> <pre><code>re.search(r'&lt;p&gt;[^&lt;/p&gt;](.*)&lt;br&gt;(.*?)&lt;/p&gt;', string, re.I) </code></pre> <p>When I use this on the first and second string, it does exactly what I want and returns me this match object:</p> <pre><code>"&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;" </code></pre> <p>But when I use this on the third string, it returns me this match object:</p> <pre><code>"&lt;p&gt;They're great&lt;/p&gt;&lt;p&gt;Yield: KA-CHING&lt;/p&gt;&lt;p&gt;Money&lt;br&gt;Money&lt;br&gt;MORE MONEY&lt;/p&gt;" </code></pre> <p>What am I screwing up?</p> <hr> <p>@Blender</p> <p>Hi Blender, this is what I came up with in grabbing the chunks I want. I'm sure there is a better way, but consider that I'm 2 weeks into Python / programming:</p> <pre><code>def get_ingredients(soup): for p in soup.find_all('p'): if p.find('br'): return p ingredients = get_ingredients(soup) p_list = soup.find_all('p') ingredient_index = p_list.index(ingredients) junk = [] junk += p_list[:ingredient_index] instructions = [] instructions += p_list[ingredient_index+1:] </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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