gpt4 book ai didi

c# - 从 html 源解析一个短字符串

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:12 25 4
gpt4 key购买 nike

<br />
Your coupon for 50% off MSRP - Inline is: XXXXXXXXXXX<br />
Your coupon for 50% off MSRP - Outdoor is: XXXXXXXXXXX<br /><br />

我想解析优惠券代码。我目前有 is(.+?)<br>但它还包括 <br>在最后。

最佳答案

尝试像这样的后视/前视模式:

".*?coupon.*?(?<=: )(\\w+)(?=<br />|<br/>)"

它将字母数字数据匹配到捕获组 1 中,该组具有单词 "coupon"并且在 ": " 之间和 "<br />"<br/>"

using System;
using System.Text.RegularExpressions;

public class Program
{
public static void Main()
{
string html = "<br />\n" +
"Your coupon for 50% off MSRP - Inline is: XXXXXXXXXXX<br />" +
"Your coupon for 50% off MSRP - Outdoor is: XXXXXXXXXXX<br /><br />";

MatchCollection matches = Regex.Matches(html, ".*?coupon.*?(?<=: )(\\w+)(?=<br />|<br/>)");
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1]);
}
}
}

结果:

XXXXXXXXXXX
XXXXXXXXXXX

Fiddle Demo

关于c# - 从 html 源解析一个短字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31736535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com