1 Reply Latest reply: Jun 24, 2013 1:48 PM by Hurukan RSS

    [java.lang.String] using matches() method

    Hurukan

      Greetings !!

       

      I'm actually trying to develop a Web interface using Java Applet.

      This Applet is expected to generate some files from templates located in the .jar files.

       

      I need some lights related to the fact that when I use this:

       

      if(strCurrentClass.matches(".G"))

      {

           for(int cpt=0;cpt<4;cpt++) WriteToPage(PageUn, iPeriode, cpt, cpt, bf_times);

      }

       

      ...on a Java 6 based NetBeans IDE it works as expected...

       

      BUT if I use the same project with Java 7 on another NetBeans IDE --> it considers that the condition is false and I have to set strCurrentClass.matches(".G.*") to obtain the same behaviour.

       

      My question is: are the regular expressions, in Java 7, differently computed by the matches() method regarding Java 6 ?

       

      That sound strange...

       

      Using my console in Linux, I tried the regular expressions using grep.

       

      [root]antares:/... # ls | grep .G

      3GT A.csv

      3GT B.csv

      4GT.csv

      5GT.csv

      6GT.csv

      [root]antares:/... # ls | grep .G.*

      3GT A.csv

      3GT B.csv

      4GT.csv

      5GT.csv

      6GT.csv

       

      ...all I need to know is that "does the current class corresponds to one of the 'G' ones" if so I have to do some process on the document before generate it on client's disk.

      As you can see both statements produced the same results, but when I use the first statement it worked only with Java 6.

       

      Is there a better way to express "if the current class name contains a digit (could be [3-6]) and then a "G" and then a "T" " ?

      I have [3-6]GT but .G for me is good ^^

        • 1. Re: [java.lang.String] using matches() method
          Hurukan

          ...after further researches I realized that matches() returns true if and only if the pattern matches in terms of content AND size...

           

          For example: grep doesn't care there is characters after the .G it found 3GT A.csv it listed this string as a matching pattern; matches() won't because it exists following characters after the .G ( A.csv).

          This is not related to Java 6 or 7 but that grep is not taking in count if there is trailing characters or not.

           

          I did [3-6]GT.* and it worked everywhere (on both java 6 and java 7 test computers).

          With the previous statement ([3-6]GT) my Applet worked only with 4GT, 5GT and 6GT (tests with 3GT A and B failed).