4 Replies Latest reply on Jul 8, 2010 9:15 PM by 843789

    RegEx error

    843789
      Hello,

      with this test code:
      //package info.cereghetti.test;
      
      import java.util.regex.Pattern;
      import java.util.regex.Matcher;
      
      public class Test {
        public static void main(String[] args) {
          String test_in = "create table blabbla(  col1 type1, col2 type3\n) fhajhdfkshfkajh";
          System.out.print(test_in+"\n");
      
           Pattern p = Pattern.compile("^[^\\(]*\\([ \\t\\n]*(.*)[ \\t\\n]*\\).*$");
          Matcher m = p.matcher(test_in);
      
          if (m.find()) {
            String st2 = m.group(1);
            System.out.println(">>>"+st2+"<<<");
           
             Pattern p2 = Pattern.compile("([^,]*)");
            Matcher m2 = p2.matcher(st2);
             while(m2.find()) {
              System.out.println("   >>>"+m2.group()+"<<<");
             }
          }
           
        }
      }
      I get:
      create table blabbla(  col1 type1, col2 type3
      ) fhajhdfkshfkajh
      
      col1 type1, col2 type3<<<
         >>>col1 type1<<<    >>><<<    >>> col2 type3<<<    >>><<<
      What I would expect in the indented part are just two lines.
      Where come the two empty lines from?

      Thank you
      qawse
        • 1. Re: RegEx error
          843789
          I suspect you need to use the DOTALL flag since the .* will not match the \n without it. The Javadoc will tell you how to use it.
          • 2. Re: RegEx error
            843789
            thank you sabre, but the modified code gives the same result:
            import java.util.regex.Pattern;
            import java.util.regex.Matcher;
            
            public class Test {
              public static void main(String[] args) {
                String test_in = "create table blabbla(  col1 type1, col2 type3\n) fhajhdfkshfkajh";
                System.out.print(test_in+"\n");
            
                 // Extract Part in (...)
                 Pattern p = Pattern.compile("^[^\\(]*\\([ \\t\\n]*(.*)[ \\t\\n]*\\).*$");
                Matcher m = p.matcher(test_in);
            
                if (m.find()) {
                  String st2 = m.group(1);
                  System.out.println(">>>"+st2+"<<<");
                 
                   // extract all ","-separated parts
                   Pattern p2 = Pattern.compile("([^,]*)", Pattern.DOTALL);
                  Matcher m2 = p2.matcher(st2);
                   while(m2.find()) {
                    System.out.println("   >>>"+m2.group()+"<<<");
                   }
                }
                 
              }
            }
            one solution is tu use split and then clean the spaces...but...
            • 3. Re: RegEx error
              843789
              The regex &#x5B;^,&#x5D;* can match an empty string. If you don't want it to do that, you should use &#x2B; instead of &#x2A;:
              Pattern p2 = Pattern.compile("([^,]+)");
              • 4. Re: RegEx error
                843789
                uncle alice, you are right. thank you!