不包含指定多个字符串!不知道怎么形容!给个例子吧 
str = pabcp,pfffp,ps1-s2,1p
我要取的是以p开始和以p结尾这一段,包含有abc和fff的不需要,得到的结果是ps1-s2,1p其中s1-s2,1是任意的。

解决方案 »

  1.   


                string str = "pabcp,pfffp,ps1-s2,1p,pabcp";
                string result = Regex.Replace(str + ",", @"(?i),*p[^p,]*?p,", "");
                Console.WriteLine(result);
      

  2.   


                string str = "pabcp,pfffp,ps1-s2,1p,pabcp";
                string result = Regex.Replace(str, @"(?i),*p[^p,]*?p,*", "");
                Console.WriteLine(result);
      

  3.   

    谢谢!能不能写一个使用Groups方式取值的正则啊!如果不行,这个也可以稍微改改使用!
      

  4.   

    我在看看有没有新的正则,ojlovecd的答案没有办法完全的取出需要的字符串!
      

  5.   

    try...            string str = "pabcp,pfffp,ps1-s2,1p";
                Regex reg = new Regex(@"(?i)p(?!,p)((?:(?!abc|fff)[^p])*)p");
                MatchCollection mc = reg.Matches(str);
                foreach (Match m in mc)
                {
                    richTextBox2.Text += m.Groups[1].Value + "\n";
                }
      

  6.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    namespace sxLdfang
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"str = pacp4 , pfffp , ps1-p2 ,1p,abcp,s1-p2  ";
                string pattern = @"(?<=[,=]\s*)(p(?![-\w]*(abc|fff))[-\w]+(?=\s*(,|$))|(?![-\w]*(abc|fff))[-\w]+p(?=\s*(,|$)))";
                MatchCollection mc = Regex.Matches(html, pattern);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
                Console.ReadKey();
            }
        }
    }
    运行结果:
    pacp4
    ps1-p2
    1p
      

  7.   


    测试这样的不行啊:
    str = pacp4 , pfffp , ps1-p2 ,1p,abcp,s1-p2