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

问题描述 投票:0回答:1

我正在尝试从下面的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>
regex
1个回答
0
投票

您的问题只是第一项前面有一个节标题范围;其余的前面有

<br>

对其中任何一个使用向后看:

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

这与您示例中的所有项目相匹配。

参见现场演示

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.