我想从以下字符串中检索特定文本。我在字符串中有一些粗体标签和段落标签。我只想检索粗体标签下的文本(...)。这是我的要求。我想存储检索字符串数组中的值。
SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd
需要在 c#.Output 中实现这一点,如下所示。
str[0] = Billgates
str[1] = SteveJobs
您可以尝试通过正则表达式解析它:
Regex expression = new Regex(@"\<b\>(.*?)\<b\>"); //This matches anything between <b> and </b>
foreach (Match match in expression.Matches(code)) //Code being the string that contains '...<b>BillGates</b>...<b>etc</b>...'
{
string value = match.Groups[1].Value;
//And from here do whatever you like with 'value'
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] strArray = new string[50];
string str = "SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd";
Regex expression = new Regex(@"\<b\>(.*?)\</b\>");
for (int i = 0; i < expression.Matches(str).Count; ++i)
{
string value = expression.Matches(str)[i].ToString();
value = value.Replace("<b>", "");
value = value.Replace("</b>", "");
strArray[i] = value;
}
Console.WriteLine(strArray[0]);
Console.WriteLine(strArray[1]);
Console.ReadLine();
}
}
}