在Java中将where子句解析为字符串
我将以下where子句作为Java中的字符串。我想把sub-select语句解析成两个字符串在Java中将where子句解析为字符串,java,regex,string,Java,Regex,String,我将以下where子句作为Java中的字符串。我想把sub-select语句解析成两个字符串 where create_dtm between ( select start_dtm FROM schema.table where table_ID = (SELECT MAX(table_ID) FROM schema.table WHERE MAINT_APP = 'JOB01')) and ( select end_dtm FROM schema.table where table_ID
where create_dtm between (
select start_dtm FROM schema.table
where table_ID = (SELECT MAX(table_ID)
FROM schema.table WHERE MAINT_APP = 'JOB01'))
and (
select end_dtm FROM schema.table
where table_ID = (SELECT MAX(table_ID)
FROM schema.table WHERE MAINT_APP = 'JOB01'))
我很难创建一个正则表达式模式来正确获取这些值,因为区分子选择的端点有点棘手 试试这个
String text = "where create_dtm between (select start_dtm FROM schema.table where table_ID = (SELECT MAX(table_ID) FROM schema.table WHERE MAINT_APP = 'JOB01'))"
+ "and (select end_dtm FROM schema.tablewhere table_ID = (SELECT MAX(table_ID) "
+ "FROM schema.table WHERE MAINT_APP = 'JOB01')) ";
Pattern p = Pattern.compile(
"where[\\w*\\s*]*\\(\\s*(select[\\w*\\s*\\.*=*\\(\\)\\']*)\\s*and\\s*\\(\\s*(select[\\w*\\s*\\.*=*\\(\\)\\']*)\\s");
Matcher m = p.matcher(text);
if (m.matches()) {
String group1 = m.group(1);
String group2 = m.group(2);
System.out.println(group1+"\n"+ group2);
}
输出
从schema.table中选择start\u dtm,其中table\u ID=select
MAXtable\u ID来自schema.table,其中MAINT\u APP='JOB01'选择
end_dtm FROM schema.table其中table_ID=从中选择MAXtable_ID
schema.table,其中MAINT_APP='JOB01'
注意:它只适用于注释中提到的当前查询您不能。可以有任意多个子查询。就这么简单。你到底想从上面的字符串中得到什么?@Shafizadeh我想创建两个字符串。一个值为:从schema.table其中table\u ID=select MAXtable\u ID FROM schema.table其中MAINT\u APP='JOB01',另一个值为:从schema.table其中table\u ID=select MAXtable\u ID FROM schema.table其中MAINT\u APP='JOB01'在[\w*\s*]*\\\\s*选择[\w*\s*\.*.*\\\\\\']*\s*和\s*\\s*选择[\w*\s*\.*\\']*\s。它只适用于您当前的查询@pragnanikinner。您能用JAVA代码本身编写该模式吗。这似乎给我带来了转义错误。