2013年10月29日星期二

java regex help intercept regex substring

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class test {
static int  i1 = 0;
public static String GetTableName(String SQL,String regex){
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(SQL);
StringBuffer sb = new StringBuffer();
while (matcher.find()) { 
            System.out.println(SQL); 
            System.out.println("rod: (" + matcher.start() + ", " + matcher.end() + ")"); 
            System.out.println("sub: " + SQL.substring(matcher.start(), matcher.end())); 
            matcher.appendReplacement(sb, ""); 
        } 
        matcher.appendTail(sb);
return null;
}

public static void main(String[] args) throws IOException{
String SQL1="create  \n table   \n \r    t1   (tc1 int primary key, tc2 int) enable primary key using index";
String regex_ct="create(\\s*)table(\\s*)(\\w*)(\\s*)\\(";
GetTableName(SQL1,regex_ct);
}
}

I want this function returns regex_ct in "(\ \ w *)" represents the name , why ?
When the string has multiple spaces , carriage returns , line feeds, or use a different table name, was still able to return to the table name ...
ask god to help, are not familiar with regular expressions ....

------ Solution ------------------------------------ --------
without regular expression should also be .

        String sql = "create  \n table   \n \r    t1   (tc1 int primary key, tc2 int) enable primary key using index";
        int index = sql.toLowerCase().indexOf("table");
        if (index != -1) {
            String temp = sql.substring(index + 5).trim();
            index = temp.indexOf(" ");
            if (index != -1) {
                System.out.println("表名:"+temp.substring(0, index));
            } else {
                System.out.println("错误的SQL");
            }
        } else {
            System.out.println("错误的SQL");
        }

------ Solution ------------------------------------- -------

  
not so simple , tens of billions of statements , I wrote one , just examples , "create \ n table \ n \ r t1 ; "keyword between the spaces, carriage returns , line feeds vary widely, not static, so I can use regular expressions in the (\ \ s *) to indicate  
solve this issue is not how to write regular , but if you can find out the applicable law . . .
no regular canonical certainly can not be written .
Regular front plus
(? s)
can ignore wrap.
According to what you say these words if you just extract t1
can do the following :

String SQL1="create  \n table   \n \r    t1   (tc1 int primary key, tc2 int) enable primary key using index";
      String regex_ct="(?s)create.*?table.*?(\\w+).*?";
      Matcher m = Pattern.compile(regex_ct).matcher(SQL1);
      while(m.find()){
       System.out.println(m.group(1));
      }

------ Solution ------------------------------------- -------

String sql="create  \n table   \n \r  \t\r\n  t1 \t  (tc1 int primary key, tc2 int) enable primary key using index";
        String regex="((?i)create)\\s*((?i)table)\\s*(.*?)\\s";
        
        Matcher m = Pattern.compile(regex).matcher(sql);
        
        if(m.find()){
         System.out.println(m.group(3));
        }

------ Solution --------------------------- -----------------
you changed on the basis of
public class test {
public static String GetTableName(String SQL,String regex){
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(SQL);
while (matcher.find()) { 
return matcher.group(3);
        } 
return null;
}

public static void main(String[] args){
String SQL1="create  \n table   \n \r    t1   (tc1 int primary key, tc2 int) enable primary key using index";
String regex_ct="create(\\s*)table(\\s*)(\\w*)(\\s*)\\(";
System.out.println(GetTableName(SQL1,regex_ct));
}
}

------ Solution ------------------------------------- -------
String sql = "create \ n table \ n \ r \ t \ r \ n t1 \ t ; (tc1 int primary key, tc2 int) enable primary key using index ";
String regex = "((? i) create) \ \ s * ((? i) table) \ \ s * (. *?) \ \ s ";

Matcher m = Pattern.compile (regex). matcher (sql);

if (m.find ()) {
System.out.println (m.group (3));
}
------ Solution ------------------- -------------------------
those of you who do not know the line is automatically generated or what . You can also put those special first remove the wrapping . If not, try this.
String SQL1 = "create   table   t1   (tc1 int primary key, tc2 int) enable primary key using index";
Matcher m2 = Pattern.compile("(?<=table).*?(?=\\()").matcher(SQL1);
while (m2.find()) {
System.out.println("---"+m2.group(0).trim()+"------");
}

------ For reference only ----------------------------------- ----
you want to return t1?
------ For reference only ------------------------- --------------

ah, yes t1
------ For reference only ------------- --------------------------


not so simple , tens of billions of statements , I wrote one , just examples , "create \ n table \ n \ r t1 ; "keyword between the spaces, carriage returns , line feeds vary widely, not static, so I can use regular expressions in the (\ \ s *) to indicate
----- - For reference only ---------------------------------------
"?" ; and the group is the corresponding use it ? The first of several " ? " On the correspondence group ( a few ) ?
I try.
Thank you

没有评论:

发表评论