这句正则表达是匹配为什么返回False?
bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", "abc*.lzh");我想实现按通配符查找匹配的文件名,上面那么些不对么?应该怎么写?

解决方案 »

  1.   

     bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", "abc.*?lzh");
      

  2.   

    bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", "abc.*?lzh");1.为什么这样可以呢?
    2.如果让用户输入*?感觉不符合用户的习惯啊,用户还是习惯于abc*.lzh.有没有办法解决呢?
      

  3.   

    bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", @"abc.*lzh");
    这样也可以
      

  4.   

                string str = "abc*.lzh";
                string result = str.Replace(".", @"\.").Replace("*", ".*");
                bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", result);
      

  5.   

    bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", "abc.*?lzh");
    bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", @"abc.*lzh");以上两个,在输入abclzh的时候,也会返回true,这就不符合要求了。
      

  6.   

    作用的地方又不同,如果你要按那样输入的话
    那你直接用
                DirectoryInfo dic = new DirectoryInfo("xxx");
                dic.GetFiles("*.txt")
    会找该文件夹下的所有txt文件。
      

  7.   


    谢谢你这么热心的回复。
    我的要求即使按通配符查找指定的文件名。
    但是是查找FTP上的文件,所以好像不能用这个
    DirectoryInfo dic = new DirectoryInfo("xxx");
    dic.GetFiles("*.txt")我感觉这个倒是符合我的要求,
    string str = "abc*.lzh";
    string result = str.Replace(".", @"\.").Replace("*", ".*");
    bool match = System.Text.RegularExpressions.Regex.IsMatch("abc-1.lzh", result);
    非常感谢!!!!!