首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java正则表达式,正则表达式截取子串

2013-10-30 
java正则表达式求助,正则表达式截取子串import java.io.IOExceptionimport java.util.regex.Matcherimpo

java正则表达式求助,正则表达式截取子串

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);
}
}

我想让这个函数返回regex_ct中“(\\w*)”所代表的名字,何解?
当字符串中有多个空格、回车、换行符或使用不同的表名时,依然能返回表名...
求大神帮忙,不太熟悉正则表达式....
java 正则表达式 regex 子串 表名提取
[解决办法]
不用正则表达式应该也可以。

        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");
        }

[解决办法]
引用:
Quote: 引用:

不用正则表达式应该也可以。

        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");


        }



不是这么简单的,几百亿条语句,我写出一条,只是实例,“create  \n table   \n \r    t1   ”关键字之间的空格、回车、换行符千差万别,不是一成不变的,所以我只能用正则表达式里的(\\s*)来表示

解决这个问题的关键不是正则怎么写,而是你能否找出来适用的规律。。。
没有规律正则肯定是写不出来的。
正则最前面加
(?s)
可以无视换行。
根据你说的这些如果只是提取t1的话
可以如下这样:

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));
      }

[解决办法]

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));
        }

[解决办法]
在你的基础上改的
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));
}
}

[解决办法]
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));
        }
[解决办法]
不知道你那些换行是自动产生还是什么。你也可以把那些特殊的换行先去除。没有的话试试这个。
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()+"------");
}

热点排行