我想用正则表达式判断中间的 斜线 public static void main(String[] args){ // start from here
查过 API 只看见这样的斜线\\ 这样斜线可以用 \\\\来匹配 。没有发现这个方向的 斜线 // 的匹配的方法。我写了3种但是都不对。希望哪位高手帮忙!在这了谢谢,并祝 新年快乐!
"public static void main(String[] args){ // start from here".matches(".+[//].+")
"public static void main(String[] args){ // start from here".matches(".+[()[]][//].+")
"public static void main(String[] args){ // start from here".matches(".+[()[]]////.+")顺便问一下 [//] 方括号什么时候必须加上,什么时候不用加?

解决方案 »

  1.   

    第一种应该是对的吧[]是Character classes
    [//]表示 '/' or '/' 
    其实就是一个'/' 所以不加这个中括号也行 更多具体用法如下
    Character classes 
    [abc] a, b, or c (simple class) 
    [^abc] Any character except a, b, or c (negation) 
    [a-zA-Z] a through z or A through Z, inclusive (range) 
    [a-d[m-p]] a through d, or m through p: [a-dm-p] (union) 
    [a-z&&[def]] d, e, or f (intersection) 
    [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction) 
    [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction) 
      

  2.   

    谢谢!我试了可是不行.import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    public class CodeCounter {
        public static long normalLines=0;
        public static long commentLines=0;
        public static long whitespaceLines=0;
        public static void main(String[] args) throws FileNotFoundException {
    //File file =new File("E:\\lbeclipse\\workspace\\2007swp\\Datenabgleich");
    File file =new File("E:\\test");
    // FileReader fr = new FileReader("E:\\eclipseALL\\workspace\\RegularExpression\\src\\EmailSpider.java");
    //         BufferedReader br =new BufferedReader(fr);
    File [] arrazFile =file.listFiles();
    for(File f:arrazFile)
    {
    if(f.getName().matches(".*\\.java$"));//(".*\\.java$"))
    {
    parse(f);
    }
    }
    System.out.println("normalLines ;"+normalLines+" commentLines: "+commentLines+" whitespaceLines : "+whitespaceLines); } private static void parse(File f) {
    FileReader fr;
    boolean b=false;
    try {
    fr = new FileReader(f);
    BufferedReader br =new BufferedReader(fr);
    String s="";
    while((s=br.readLine())!= null)
    {
    s=s.trim();
      if(s.matches("^[\\s&&[^\\n]]*$"))//whitespace
      {
      whitespaceLines++;
      }
      else if(s.startsWith("/*")&& !s.endsWith("*/"))
      {
      commentLines++;
      b=true;
      }
      else if(s.startsWith("/*")&& s.endsWith("*/")){
      commentLines++;
      }
      else if(b==true){
      commentLines++; 
      if(s.endsWith("*/"))
      {
      b=false;
      }
      }
    //   else if(s.startsWith("//")||s.matches(".+[//][.]+")){
    //   commentLines++; 
    //   }
      else{
      normalLines++; 
      }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
            
    }}
    //我想用正则表达式判断中间的 斜线 public static void main(String[] args){ // start from here
    //"public static void main(String[] args){ // start from here".matches(".+\\[//][.]+")
      

  3.   

    如归你只想判断那一行是否有 // 可以这样做
    String str = "public static void main(String[] args){ // start from here";
    str.matches(".*?//(.*?)"