0
<tr>
11:15
12:15
13:15
</tr>

<tr>
18:15
19:15
20:15
</tr>

in this case output should be: [ (11:15, 12:15, 13:15), (18:15, 19:15, 20:15) ]

My pattern: (\d\d:\d\d)[\s\S]*?(\d\d:\d\d)[\s\S]*?(\d\d:\d\d)[\s\S]*?</tr> will work only if there are 3 hours in each tr tag

But this should work if there are 1-3 hours (in the same format \d\d:\d\d) in each tr tag. Another example. For this my pattern doesn't work anymore.

<tr>12:00 13:00</tr>
<tr>14:00 15:00 16:00</tr>
<tr>12:00</tr>

Output should be: [ (12:00, 13:00, ), (14:00, 15:00, 16:00), (12:00, , ) ]

And here's another thing: every hour isn't separated by just whitespaces, the real file looks like this: I used [\s\S]*? or [\w\s<>="-/:;?|]*? for this. An hour is either in simple span or in longer form .

example:

<tr>
<span class="na">16:00</span>
<span>|</span><a href="http:/21.28.147.68/msi/default.aspx?event_id=52514&amp;typetran=1&amp;ReturnLink=http://www.kino.pl/kina/przedwiosnie/repertuar.php" class="toolBox" data-hasqtip="true" aria-describedby="qtip-0">20:45</td>
</tr>
kierrez
  • 3
  • 1
  • 2

3 Answers3

1

I would parse the HTML with an HTML parser, find all tr elements in the table and split the contents or each row using str.split() - it would handle both spaces and newlines. Example using BeautifulSoup parser:

from bs4 import BeautifulSoup

data = """
<table>
    <tr>
    11:15
    12:15
    13:15
    </tr>

    <tr>
    18:15
    19:15
    20:15
    </tr>

    <tr>12:00 13:00</tr>
    <tr>14:00 15:00 16:00</tr>
    <tr>12:00</tr>
</table>"""

soup = BeautifulSoup(data, "html.parser")

result = [row.text.split() for row in soup.table.find_all("tr")]
print(result)

Prints:

[['11:15', '12:15', '13:15'], 
 ['18:15', '19:15', '20:15'], 
 ['12:00', '13:00'], 
 ['14:00', '15:00', '16:00'], 
 ['12:00']]

An hour is either in simple span or in longer form .

This is even better, let's find every inner element inside a tr matching a specific pattern and get the text

[[elm.strip() for elm in row.find_all(text=re.compile(r"\d\d:\d\d"))] 
 for row in soup.table.find_all("tr")]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

If you'd prefer regex, you could use this:

found = []
for group in re.findall(r'(\d\d:\d\d.*){1,3}</tr>', data, re.DOTALL):
    found.append(re.findall(r'(\d\d:\d\d)', group, re.DOTALL))
# found == [['12:00', '13:00'], ['14:00', '15:00', '16:00'], ['12:00']]
Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
0

Try this solution using regex:

import re

input = """
<tr>
11:15
12:15
13:15
</tr>

<tr>
18:15
19:15
20:15
</tr>

<tr>12:00 13:00</tr>
<tr>14:00 15:00 16:00</tr>
<tr>12:00</tr>
"""

print [ re.findall('(\d\d:\d\d)', tr) for tr in re.findall('<tr>([^<]*)</tr>', input)] 

Output:

[['11:15', '12:15', '13:15'], 
 ['18:15', '19:15', '20:15'], 
 ['12:00', '13:00'], 
 ['14:00', '15:00', '16:00'], 
 ['12:00']]
Quinn
  • 4,394
  • 2
  • 21
  • 19