Regularexpression not working.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Regularexpression not working.

    Code:
    String text = "/** */test/** */test";
    System.out.println(text.replaceAll("/\\*\\*.*\\*/"));
    It prints test instead of testtest. How regular expression is working here?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    It looks like the largest possible substring is being returned in the match, so the substring " */test/** " is matched by .*

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Then how should I remove /** */? Actually i m trying to remove the documentation part from my JS file when i expose this on web.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        There's a good description of regular expressions at http://www.regular-expressions.info/. (The * is being greedy.)

        Alteratively, since slash-star comments don't nest you could just use String's indexOf().

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Yea .. i solved the problem.

          Code:
          while((line = br.readLine()) != null){
                      js_text.append(line.trim().replaceAll("//.*", "")); //gets the file text(removed comments //)
                  }
          Code:
          int start=0,end=0 ;
                  while((start = js_text.indexOf("/*")) != -1){
                      end = js_text.indexOf("*/");
                      if(end != -1){
                          js_text.replace(start, end+2, "");
                      }
                  }
          Thanks a lot ...

          Comment

          Working...