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

aio_o 2013-10-28 10:55:30
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*)”所代表的名字,何解?
当字符串中有多个空格、回车、换行符或使用不同的表名时,依然能返回表名...
求大神帮忙,不太熟悉正则表达式....
...全文
260 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
aio_o 2013-10-29
  • 打赏
  • 举报
回复
"?"和group是对应使用的么?第几个"?"就对应group(几)? 我试试。 多谢大家
tony4geek 2013-10-29
  • 打赏
  • 举报
回复
不知道你那些换行是自动产生还是什么。你也可以把那些特殊的换行先去除。没有的话试试这个。
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()+"------");
		}
基础学起 2013-10-29
  • 打赏
  • 举报
回复
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)); }
末日哥 2013-10-29
  • 打赏
  • 举报
回复
在你的基础上改的
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));
	}
}
小绵羊 2013-10-29
  • 打赏
  • 举报
回复

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));
        }
失落夏天 2013-10-29
  • 打赏
  • 举报
回复
引用 4 楼 aio_o 的回复:
[quote=引用 2 楼 ghostkngiht 的回复:] 不用正则表达式应该也可以。

        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*)来表示[/quote] 解决这个问题的关键不是正则怎么写,而是你能否找出来适用的规律。。。 没有规律正则肯定是写不出来的。 正则最前面加 (?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));
	      }
aio_o 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 ghostkngiht 的回复:
不用正则表达式应该也可以。

        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*)来表示
aio_o 2013-10-29
  • 打赏
  • 举报
回复
引用 1 楼 fudongrifdr 的回复:
你是想返回t1?
嗯,是t1
ghostkngiht 2013-10-28
  • 打赏
  • 举报
回复
不用正则表达式应该也可以。

        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");
        }
末日哥 2013-10-28
  • 打赏
  • 举报
回复
你是想返回t1?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧