gpt4 book ai didi

c# - Regex 和 span 类如何在此代码中工作?

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:42 24 4
gpt4 key购买 nike

我继承了一个网站的代码,这个特殊的函数用于在给定部件号时从网站获取描述。我以前从未使用过正则表达式,所以这个集合有点超出我的范围,并且希望得到一些帮助来弄清楚为什么它不能正常工作。

本质上,此功能的理想操作是,当站点的用户在适当的字段中输入零件编号并按下按钮时,将从单独的站点获得的标准零件描述输出给用户.我检查了正则表达式试图匹配的第三方网站上的元素,它被编码为

<span id="ctl00_BodyContentPlaceHolder_lblDescription">Random Description</span>
public static string GetPartHpDescription(string url)
{

// Create a request to the url
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

// If the request wasn't an HTTP request (like a file), ignore it
if (request == null) return null;

// Use the user's credentials
request.UseDefaultCredentials = true;

// Obtain a response from the server, if there was an error, return nothing
HttpWebResponse response = null;
try { response = request.GetResponse() as HttpWebResponse; }
catch (WebException) { return null; }

// Regular expression for an HTML title
// string regex = @"(?<=<body.*>)([Description : HP]*)(?=</body>)";
string regex = "<span [^>]*id=(\"|')ctl00_BodyContentPlaceHolder_lblDescription(\"|')>(.*?)</span>";
string regex1 = "<span [^>]*id=(\"|')ctl00_BodyContentPlaceHolder_gvGeneral_ctl02_lblpartdesc1(\"|')>(.*?)</span>";
// Regex re = new Regex(@"<span\s+id=""ctl00_BodyContentPlaceHolder_lblDescription");
// string regex = @"<span\s+id=""ctl00_BodyContentPlaceHolder_lblDescription"
// If the correct HTML header exists for HTML text, continue
if (new List<string>(response.Headers.AllKeys).Contains("Content-Type"))
if (response.Headers["Content-Type"].StartsWith("text/html"))
{
// Download the page
WebClient web = new WebClient();
web.UseDefaultCredentials = true;
string page = web.DownloadString(url);
// string title = Regex.Match(page, @"<span\s+id=""ctl00_BodyContentPlaceHolder_lblDescription"">.*?</span>", RegexOptions.IgnoreCase).Groups["Title"].Value;
// Extract the title
Regex ex = new Regex(regex, RegexOptions.IgnoreCase);
String data = ex.Match(page).Value.Trim();
if (data == "")
{
Regex ex1 = new Regex(regex1, RegexOptions.IgnoreCase);
data = ex1.Match(page).Value.Trim();
}
return data;
// return title;
}

// Not a valid HTML page
return null;
}

当前发生的情况是,如果零件号当前不在系统数据库(sql 后端)中,则该函数无法正确获取零件描述。

最佳答案

我的猜测是我们有一些 ID 希望提取它们的 textContnet,如果我们必须使用正则表达式来这样做,我们将从一个简单的表达式开始,然后如果有必要,我们将添加更多约束,

<span id=["'](ctl00_.+|other_ids)["']>(.+?)<\/span>

Demo

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"<span id=[""'](ctl00_.+|other_ids)[""']>(.+?)<\/span>";
string input = @"<span id=""ctl00_BodyContentPlaceHolder_lblDescription"">Random Description</span>
<span id='ctl00_BodyContentPlaceHolder_lblDescription'>Random Description</span>
";
RegexOptions options = RegexOptions.Multiline;

foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}

正则表达式电路

jex.im可视化正则表达式:

enter image description here

关于c# - Regex 和 span 类如何在此代码中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529332/

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