java正则挖取建表语句
java正则挖取建表语句:
-------------------------------------------------
-- Export file for user SSTEST --
-- Created by SINO_PING on 2012-7-30, 18:16:27 --
-------------------------------------------------
spool ciinsureriskwarning.log
prompt
prompt Creating table CIINSURERISKWARNING
prompt ==================================
prompt
create table CIINSURERISKWARNING
(
DEMANDNO VARCHAR2(50) not null,
SERIALNO NUMBER not null,
RISKWARNINGTYPE VARCHAR2(2),
CLAIMCODE VARCHAR2(50),
COMPANYID VARCHAR2(8),
ACCIDENTTIME VARCHAR2(20),
ACCIDENTPLACE VARCHAR2(100)
)
;
comment on column CIINSURERISKWARNING.DEMANDNO
is '查询码';
comment on column CIINSURERISKWARNING.SERIALNO
is '序号';
comment on column CIINSURERISKWARNING.RISKWARNINGTYPE
is '风险警示类型';
comment on column CIINSURERISKWARNING.CLAIMCODE
is '理赔编号';
comment on column CIINSURERISKWARNING.COMPANYID
is '保险公司代码';
comment on column CIINSURERISKWARNING.ACCIDENTTIME
is '出险时间';
comment on column CIINSURERISKWARNING.ACCIDENTPLACE
is '查询码';
alter table CIINSURERISKWARNING
add constraint PK_CIINSURERISKWARNING primary key (DEMANDNO, SERIALNO);
create table CIINSURERISKWARNING
(
DEMANDNO VARCHAR2(50) not null,
SERIALNO NUMBER not null,
RISKWARNINGTYPE VARCHAR2(2),
CLAIMCODE VARCHAR2(50),
COMPANYID VARCHAR2(8),
ACCIDENTTIME VARCHAR2(20),
ACCIDENTPLACE VARCHAR2(100)
)
;
spool off
我的代码
Pattern p = Pattern.compile("(create table \\w+\\s*\\(.+\\);)");
Matcher m = p.matcher(buffer.toString());
while(m!=null && m.find()) {
System.out.println(m.group());
}
输出结果:
create table CIINSURERISKWARNING( DEMANDNO VARCHAR2(50) not null, SERIALNO NUMBER not null, RISKWARNINGTYPE VARCHAR2(2), CLAIMCODE VARCHAR2(50), COMPANYID VARCHAR2(8), ACCIDENTTIME VARCHAR2(20), ACCIDENTPLACE VARCHAR2(100));comment on column CIINSURERISKWARNING.DEMANDNO is '查询码';comment on column CIINSURERISKWARNING.SERIALNO is '序号';comment on column CIINSURERISKWARNING.RISKWARNINGTYPE is '风险警示类型';comment on column CIINSURERISKWARNING.CLAIMCODE is '理赔编号';comment on column CIINSURERISKWARNING.COMPANYID is '保险公司代码';comment on column CIINSURERISKWARNING.ACCIDENTTIME is '出险时间';comment on column CIINSURERISKWARNING.ACCIDENTPLACE is '查询码';alter table CIINSURERISKWARNING add constraint PK_CIINSURERISKWARNING primary key (DEMANDNO, SERIALNO);create table CIINSURERISKWARNING( DEMANDNO VARCHAR2(50) not null, SERIALNO NUMBER not null, RISKWARNINGTYPE VARCHAR2(2), CLAIMCODE VARCHAR2(50), COMPANYID VARCHAR2(8), ACCIDENTTIME VARCHAR2(20), ACCIDENTPLACE VARCHAR2(100));
我想要的是只把建表语句挖出来,如下:
create table CIINSURERISKWARNING( DEMANDNO VARCHAR2(50) not null, SERIALNO NUMBER not null, RISKWARNINGTYPE VARCHAR2(2), CLAIMCODE VARCHAR2(50), COMPANYID VARCHAR2(8), ACCIDENTTIME VARCHAR2(20), ACCIDENTPLACE VARCHAR2(100));
请高手指点一下,我的正则表达式为什么达不到目标那,谢谢
------解决方案--------------------
Java code
Pattern p = Pattern.compile("(create table \\w+\\s*\\(.+?\\);)");
Matcher m = p.matcher(buffer.toString());
while(m!=null && m.find()) {
System.out.println(m.group());
}
------解决方案--------------------
即然goldenfish1919比较忙,那我就说下我的理解:
主要说下.+和.+?的区别
一般默认情况下,正则的量词是贪婪的,也就是“尽可能多地匹配”,举个例子说,比如"a+"这个正则,对于"aaaaaab"这个字符串,它就会匹配到6个a,对于你的建表语句而言,当匹配到第一个create table结束时,并不会立即结束,而是还去尝试寻找尽可能多的匹配结果,所以他找啊找,找到了第二个create table,如果有三个create table的话,他也一直会找到第三个