我目前正在使用从文件中读取的行手动编写此代码,并尝试读取表开始处的所有表 ddl a_
这个的输入:
Other stuff:
Other stuff:
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
stuff
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Other stuff:
create table b_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
other stuff
other stuff
应该只输出这个
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
目前我正在使用 LineReader 并记住何时看到 create table
然后读取所有内容直到看到 )
这是最有效的方法吗?我可以使用一些花哨的 reg ex 吗?
我尝试了以下 reg ex 但这没有用,因为它只是再次返回整个字符串。也许新线路正在打破它
"^.*create.*a_(.*?)\\).*$", "$1")
如有任何建议,我们将不胜感激
谢谢
最佳答案
尝试这样的事情:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copyLarge(getClass().getClassLoader().getResourceAsStream("input.txt"), baos);
String org = baos.toString();
final Pattern compile = Pattern.compile("(?s)(create table a_.*?\n\\)\n)");
final Matcher matcher = compile.matcher(org);
while (matcher.find()) {
System.out.println(matcher.group());
}
输入.txt
Other stuff:
Other stuff:
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
stuff
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Other stuff:
create table b_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
other stuff
输出
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
关于java - 用于读取 'create table' sql 语句的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127247/