我有几段文字  现要根据对文字的标记进行截取例如:文本域内容如下
#A#
<#A#>abcdefg</#A#>
#B#
<#B#>1234567</#B#>
#C#
<#C#>ABCDEFG</#C#>
#D#
<#D#>7654321</#D#>现在我想输出:1234567 7654321程序上该怎么写请大家帮忙

解决方案 »

  1.   

    用正则提取 <#D#>和</#D#>间的文字
      

  2.   

    或者直接提取连续数字            System.IO.StreamReader reader = new System.IO.StreamReader("e:\\1.txt",System.Text.Encoding.Default);
                string str = reader.ReadToEnd();
                Regex reg = new Regex(@"(?is)[\d]+");
                MatchCollection mc = reg.Matches(str);
                foreach (Match m in mc)
                {
                    MessageBox.Show(m.Value);
                }
      

  3.   


      string source = @"
    #A#
    <#A#>abcdefg</#A#>
    #B#
    <#B#>1234567</#B#>
    #C#
    <#C#>ABCDEFG</#C#>
    #D#
    <#D#>7654321</#D#>
    ";
                foreach (Match m in Regex.Matches(source, @"(#(B|D)#)\s*<\1>(.*?)</\1>"))
                {
                    Console.WriteLine(m.Groups[3].Value);
                }
      

  4.   

    4楼 版主Chinajiyong 已经解决了问题追问一下
    如果文本内容为
    <#A#>abcdefg</#A#>
    <#B#>1234567</#B#>
    <#C#>ABCDEFG</#C#>
    <#D#>7654321</#D#>正则该怎么写谢谢
      

  5.   

    正则改成<(#(B|D)#)>(.*?)</\1>
      

  6.   

    如果以上两种满足的话            string source = @"
    #A#
    <#A#>abcdefg</#A#>
    #B#
    <#B#>1234567</#B#>
    #C#
    <#C#>ABCDEFG</#C#>
    #D#
    <#D#>7654321</#D#>
    <#B#>1234567</#B#>
    ";
                foreach (Match m in Regex.Matches(source, @"(#(B|D)#\s)*?<(#(B|D)#)>(.*?)</\3>"))
                {
                    Console.WriteLine(m.Groups[5].Value);
                }
      

  7.   

    用正则即可LS上  也许LZ不一定是要提取数字呢  只想要标记里面的东西饿
      

  8.   

    根据标记?你的意思是说是要输入一个标记,然后在输出相应的内容是吧?
    假设你输入 A,B,C,D这种规范的标记string lbl = Console.ReadLine();
    Match ma = Reges.IsMatch(source,"<#"+lbl+"#>([0-9]+?)</#"+lbl+"#>");
    Console.WriteLine(ma.Groups[1].value);