C# 如何从c语言中的给定字符串中检索特定文本#
我想从以下字符串中检索特定文本。字符串中有一些粗体标记和段落标记。我只想检索粗体标记(…)下的文本。这是我的要求。我想将检索到的值存储在字符串数组中C# 如何从c语言中的给定字符串中检索特定文本#,c#,asp.net,C#,Asp.net,我想从以下字符串中检索特定文本。字符串中有一些粗体标记和段落标记。我只想检索粗体标记(…)下的文本。这是我的要求。我想将检索到的值存储在字符串数组中 SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd 您可以尝试通过正则表达式对其进行解析: Regex expression = new Regex(@"\<b\>(.*?)
SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd
您可以尝试通过正则表达式对其进行解析:
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'
}
Regex表达式=新的Regex(@“\(.*?\”)//这与和之间的任何内容都匹配
foreach(Match-in-expression.Matches(code))//代码是包含“…BillGates…等”的字符串
{
字符串值=匹配。组[1]。值;
//从这里开始,用“价值”做任何你喜欢的事情
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
string[]strArray=新字符串[50];
string str=“SampleTextBillgates这是Parastevejobsthisend”;
正则表达式=新正则表达式(@“\(.*?\”);
for(int i=0;i
你的意思是match.Groups[1]。我想是Value。编辑:哎呀,看来我误解了你,你是对的。是的!
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();
}
}
}