+ -
当前位置:首页 → 问答吧 → 一个正则问题!!!

一个正则问题!!!

时间:2011-12-20

来源:互联网

学习高级正则遇到2个表达式的问题!

C# code
 public static void GetA7() 
        {
            string str = "windows 97,windows 98,windows xp,windows server 2003,windows 7,windows server 2008,linux,niunix";
            Regex regex = new Regex(@"(?<!windows)[^,]+");
            Console.WriteLine("哪些不是windows系统:");
            foreach (Match match in regex.Matches(str)) 
            {
                Console.WriteLine(match);
            }
        }


C# code
public static void GetA9() 
        {
            string str = "长沙是湖南的,岳阳是湖南的,湘潭是湖南的,株洲是湖南的,郴州是湖南的,武汉是湖北的,深圳是广东的";
            //Regex regex = new Regex(@"[\u4e00-\u9fa5]+(?!是湖南的)");
            Regex regex = new Regex(@"[^,]+(?!是湖南的)");
            Console.WriteLine("下面不是湖南的城市:");
            foreach (Match match in regex.Matches(str)) 
            {
                Console.WriteLine(match);
            }
        }


肯定的(?<=)和(?=)都可以通过!!
否定的不知道为什么出了问题!!!

作者: chaichangaini   发布时间: 2011-12-20

坐等正则高手!

作者: chaichangaini   发布时间: 2011-12-20

没人啊!!! 自己顶啊!

作者: chaichangaini   发布时间: 2011-12-20

其实我认为楼主就是正则高手。。。

作者: jianghui7897   发布时间: 2011-12-20

引用 3 楼 jianghui7897 的回复:

其实我认为楼主就是正则高手。。。

晕 在学习!
也是菜鸟啊!

作者: chaichangaini   发布时间: 2011-12-20

加分了 100分全送上!! 高手帮忙解决下!!

作者: chaichangaini   发布时间: 2011-12-20

C# code

void Main()
{
     string str = "windows 97,windows 98,windows xp,windows server 2003,windows 7,windows server 2008,linux,niunix";
            Regex regex = new Regex(@"^(?!windows).+$");
            Console.WriteLine("哪些不是windows系统:");
            foreach (var s in str.Split(',')) 
            {
                if(regex.IsMatch(s))
                Console.WriteLine(regex.Match(s).Value);
            }
 
}


void Main()
{
    string str = "长沙是湖南的,岳阳是湖南的,湘潭是湖南的,株洲是湖南的,郴州是湖南的,武汉是湖北的,深圳是广东的";
            Regex regex = new Regex(@"^.+(?<!是湖南的)$");
            Console.WriteLine("下面不是湖南的城市:");
            foreach (var s in str.Split(',')) 
            {
                if(regex.IsMatch(s))
                Console.WriteLine(regex.Match(s).Value);
            }
            
         

}




资料参考: http://blog.csdn.net/lxcnn/article/details/4304754
  http://blog.csdn.net/lxcnn/article/details/4954134

作者: q107770540   发布时间: 2011-12-20

引用 6 楼 q107770540 的回复:

C# code

void Main()
{
string str = "windows 97,windows 98,windows xp,windows server 2003,windows 7,windows server 2008,linux,niunix";
Regex regex = new Regex(@"^(?!windows).+$");
……

问下这种(?!) (?<!)只匹配^$这种完全满足的情况吗?

作者: chaichangaini   发布时间: 2011-12-20

相关阅读 更多