正则表达式根据特定条件

问题描述:

我有这个字符串正则表达式根据特定条件

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

使用'] *>([^ ' –

+0

它不是为我工作:( –

+0

闯入4件 –

为了让您的输入的第二部分中,我加入了代码的一部分 - 这里是完整的代码

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

这个程序会给第二个输出没有标签,看起来像你已经编辑的问题,并添加标签也在第二部分输出 – Aby

增加这个答案,因为它会帮助你找到确切的答案,你想要的(在你的第二个部分标签)

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(); 
} 
}