匹配所有的 英文+ 数字 命名 的 txt文件的名称。
例子:a.txt
A.txt
a0.txt
0a.txt
a.TXT
A.TXT
a0.TXT
0a.TXT

用一则正则表达式描述一下

解决方案 »

  1.   

    public static boolean isTxt(String str)
        {
            str = str.trim();
            String arg = "^[a-zA-Z_][a-zA-Z0-9_]{0,}[.][tT][xX][tT]$";
            return str.matches(arg);
        }
      

  2.   

    结果
    a.txt   true
    A.txt   true
    a0.tXt  true
    0A.txt  false
      

  3.   

    fileName.matches("\\w+\.[tT][xX][tT]")
      

  4.   

    不好意思,上面少了个"\"
    fileName.matches("\\w+\\.[tT][xX][tT]")
      

  5.   

    上面的有错
    fileName.matches("\\w+\\.[tT][xX][tT]")少了一个\我还以为楼主不要数字开头的...
    所以把0a.txt这种判断为false了...
    而且还加了下划线。