[英]How to retrieve specific text from the given string in the c#
我想從以下字符串中檢索特定文本。我在字符串中有一些粗體標簽和段落標簽。我只想檢索在粗體標簽( ... )下的文本。這是我的要求。我想存儲在字符串數組中檢索到的值。
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.