正则表达式解析Html如何重复代码块

时间:2011-10-31 10:20:14

标签: regex

我正在尝试从下面的html中添加食物条目,当我使用正则表达式时它似乎只抓住每个部分的第一个食物条目,我想知道如何重复逻辑来抓取所有行中的所有行部分?另外,如果每个行都不能使用点部分可选?

任何帮助将不胜感激

输出:

<span class="sectiontitle">Medium Pan Pizza - 1 slice / 1/8th pizza</span><br>\r\n12" Medium Pan Pizza: Cheese\r\n    (270 cal/13 g fat/1 g fiber/27 g carbs) <span class="blackpts">  6.5</span><br>   <span class="sectiontitle">Medium Pan Pizza - 1 slice / 1/8th pizza</span><br>\r\n12" Medium Pan Pizza: Cheese\r\n    (270 cal/13 g fat/1 g fiber/27 g carbs) <span class="blackpts">  6.5</span><br>   Medium Pan Pizza - 1 slice / 1/8th pizza</span><br> 12" Medium Pan Pizza: Cheese    (270 cal/13 g fat/1 g fiber/27 g carbs) <span class="blackpts">  6.5</span><br>
<span class="sectiontitle">Medium Thin 'N Crispy Pizza - 1 slice / 1/8th pizza</span><br>\r\n12" Medium Thin 'N Crispy Pizza: Cheese\r\n    (200 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>  <span class="sectiontitle">Medium Thin 'N Crispy Pizza - 1 slice / 1/8th pizza</span><br>\r\n12" Medium Thin 'N Crispy Pizza: Cheese\r\n    (200 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>  Medium Thin 'N Crispy Pizza - 1 slice / 1/8th pizza</span><br>  12" Medium Thin 'N Crispy Pizza: Cheese (200 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>
<span class="sectiontitle">Medium Hand-Tossed Pizza - 1 slice / 1/8th pizza</span><br>\r\n12" Medium Hand-Tossed Pizza: Cheese\r\n    (230 cal/10 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 5 </span><br>

正则表达式:

<span class="sectiontitle">(?<Name>.*)\s*(?<ServingName>.*)\s*(?<ServingInfo>\((?<Calories>.*?) cal/(?<Fat>.*?) g fat/(?<Fiber>.*?) g fiber/(?<Carbs>.*?) g carbs\) <span class="blackpts">\s*(?<Points>.*?)\s*</span>)

HTML:

<p><span class="sectiontitle">Medium Pan Pizza - 1 slice / 1/8th pizza</span><br>
12" Medium Pan Pizza: Cheese
    (270 cal/13 g fat/1 g fiber/27 g carbs) <span class="blackpts">  6.5</span><br>
12" Medium Pan Pizza: Pepperoni
    (280 cal/14 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Pan Pizza: Supreme
    (310 cal/16 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Pan Pizza: Pepperoni & Mushroom
    (260 cal/13 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Pan Pizza: Italian Sausage & Red Onion
    (300 cal/15 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Pan Pizza: Quartered Ham & Pineapple
    (250 cal/11 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Pan Pizza: Meat Lover's
    (370 cal/22 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 9 </span><br>
12" Medium Pan Pizza: Veggie Lover's
    (250 cal/11 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Pan Pizza: Super Supreme
    (330 cal/17 g fat/2 g fiber/29 g carbs) <span class="blackpts"> 7.5 </span><br>
12" Medium Pan Pizza: Chicken Supreme
    (270 cal/12 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Pan Pizza: Pepperoni Lover's
    (330 cal/18 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 8 </span><br>
12" Medium Pan Pizza: Sausage Lover's
    (360 cal/20 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 8.5 </span><br>
12" Medium Pan Pizza: Bacon
    (300 cal/16 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Pan Pizza: Beef
    (300 cal/15 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Pan Pizza: Chicken
    (280 cal/12 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Pan Pizza: Ham
    (250 cal/11 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Pan Pizza: Italian Sausage
    (320 cal/18 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 7.5 </span><br>
12" Medium Pan Pizza: Pork
    (310 cal/16 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Pan Pizza: Black Olives
    (260 cal/12 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Pan Pizza: Pineapple
    (250 cal/11 g fat/1 g fiber/29 g carbs) <span class="blackpts"> 5.5 </span><br>
    <span class="sectiontitle">Medium Thin 'N Crispy Pizza - 1 slice / 1/8th pizza</span><br>
12" Medium Thin 'N Crispy Pizza: Cheese
    (200 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Pepperoni
    (210 cal/10 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Thin 'N Crispy Pizza: Supreme
    (230 cal/11 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Pepperoni & Mushroom
    (190 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Italian Sausage & Red Onion
    (230 cal/11 g fat/1 g fiber/23 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Quartered Ham & Pineapple
    (180 cal/6 g fat/1 g fiber/23 g carbs) <span class="blackpts"> 4 </span><br>
12" Medium Thin 'N Crispy Pizza: Meat Lover's
    (310 cal/18 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 7.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Veggie Lover's
    (180 cal/7 g fat/1 g fiber/23 g carbs) <span class="blackpts"> 4 </span><br>
12" Medium Thin 'N Crispy Pizza: Super Supreme
    (250 cal/13 g fat/1 g fiber/23 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Thin 'N Crispy Pizza: Chicken Supreme
    (200 cal/7 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Pepperoni Lover's
    (260 cal/14 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Thin 'N Crispy Pizza: Sausage Lover's
    (280 cal/16 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Bacon
    (240 cal/13 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Beef
    (230 cal/11 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Chicken
    (210 cal/8 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Ham
    (180 cal/7 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 4 </span><br>
12" Medium Thin 'N Crispy Pizza: Italian Sausage
    (250 cal/13 g fat/1 g fiber/21 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Thin 'N Crispy Pizza: Pork
    (230 cal/12 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Black Olives
    (190 cal/8 g fat/1 g fiber/22 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Thin 'N Crispy Pizza: Pineapple
    (180 cal/6 g fat/1 g fiber/24 g carbs) <span class="blackpts"> 4 </span><br>
    <span class="sectiontitle">Medium Hand-Tossed Pizza - 1 slice / 1/8th pizza</span><br>
12" Medium Hand-Tossed Pizza: Cheese
    (230 cal/10 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Hand-Tossed Pizza: Pepperoni
    (240 cal/11 g fat/1 g fiber/24 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Hand-Tossed Pizza: Supreme
    (270 cal/13 g fat/2 g fiber/26 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Hand-Tossed Pizza: Pepperoni & Mushroom
    (230 cal/9 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Hand-Tossed Pizza: Italian Sausage & Red Onion
    (260 cal/12 g fat/1 g fiber/26 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Hand-Tossed Pizza: Quartered Ham & Pineapple
    (220 cal/8 g fat/1 g fiber/26 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Hand-Tossed Pizza: Meat Lover's
    (340 cal/19 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 8 </span><br>
12" Medium Hand-Tossed Pizza: Veggie Lover's
    (210 cal/8 g fat/2 g fiber/26 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Hand-Tossed Pizza: Super Supreme
    (280 cal/13 g fat/2 g fiber/29 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Hand-Tossed Pizza: Chicken Supreme
    (230 cal/7 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Hand-Tossed Pizza: Pepperoni Lover's
    (290 cal/13 g fat/1 g fiber/28 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Hand-Tossed Pizza: Sausage Lover's
    (310 cal/16 g fat/2 g fiber/28 g carbs) <span class="blackpts"> 7 </span><br>
12" Medium Hand-Tossed Pizza: Bacon
    (260 cal/13 g fat/1 g fiber/24 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Hand-Tossed Pizza: Beef
    (260 cal/12 g fat/2 g fiber/25 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Hand-Tossed Pizza: Chicken
    (240 cal/9 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 5.5 </span><br>
12" Medium Hand-Tossed Pizza: Ham
    (210 cal/6 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 4.5 </span><br>
12" Medium Hand-Tossed Pizza: Italian Sausage
    (280 cal/14 g fat/1 g fiber/25 g carbs) <span class="blackpts"> 6.5 </span><br>
12" Medium Hand-Tossed Pizza: Pork
    (270 cal/13 g fat/2 g fiber/25 g carbs) <span class="blackpts"> 6 </span><br>
12" Medium Hand-Tossed Pizza: Black Olives
    (220 cal/9 g fat/2 g fiber/25 g carbs) <span class="blackpts"> 5 </span><br>
12" Medium Hand-Tossed Pizza: Pineapple
    (210 cal/8 g fat/1 g fiber/27 g carbs) <span class="blackpts"> 4.5 </span><br>
</p>

0 个答案:

没有答案