我有这个字符串
You have 6 uncategorized contacts from <an id='316268655'>SAP SE</an>
我想收集2部分字符串
you have 6 uncategorised contacts from
<an >Sap SE </an>
下面没有id属性尝试正在运行
var parts = Regex.Split(value, @"(<an[\s\S]+?<\/an>)").Where(l => l != string.Empty).ToArray();
但是从时间属性ID即将来临,我无法解析它。
任何人都可以用语法帮助我
答案 0 :(得分:2)
要获得输入的第二部分,我之后添加了代码部分 - 这是完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace PatternMatching
{
public class Program
{
public static void Main(string[] args)
{
string input = "You have 6 uncategorized contacts from <an id='316268655'>SAP SE</an>";
var parts = Regex.Split(input, @"(<an[\s\S]+?<\/an>)").Where(l => l != string.Empty).ToArray();
foreach(var a in parts)
{
Console.WriteLine(a);
break;
}
string pattern = "<an.*?>(.*?)<\\/an>";
MatchCollection matches = Regex.Matches(input, pattern);
if (matches.Count > 0)
foreach (Match m in matches)
Console.WriteLine(m.Groups[1]);
Console.ReadLine();
}
}
}
答案 1 :(得分:1)
添加此答案,因为它可以帮助您找到所需的确切答案(第二部分中的标记)
public class Program
{
public static void Main(string[] args)
{
string input = "You have 6 uncategorized contacts from <an id='316268655'>SAP SE</an>";
var parts = Regex.Split(input, @"(<an[\s\S]+?<\/an>)").Where(l => l != string.Empty).ToArray();
string part="";
foreach(var a in parts)
{
part =a;
if(a.Contains("<an")){
part = Regex.Replace(a, @"(?i)<(an)(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>", "<$1>");
}
Console.WriteLine(part);
}
Console.ReadLine();
}
}