因为在SAP Hana中,“ inner”关键字是一个保留关键字,所以我正在寻找Java regex用我的Hana查询中的新关键字替换所有出现的inner关键字,而使内部联接和“ as internal”保持不变
下面是我输入的Hana查询:
select INNER.*, INNER.stud_id as "inner", INNER . stud_id
from (select NVL(qs.qual_id,qr.qual_id) AS qualIDRoot,qr.stud_id, qr.qual_id as qualID
from pa_stud_qual_relation qr,
pa_qual_subqual qs
where 1 = 1
and qs.qual_id_sub(+)=qr.qual_id
group by qr.stud_id, qr.qual_id, NVL(qs.qual_id,qr.qual_id)
) INNER
inner join pa_qual_cpnt qc on INNER.qualid=qc.qual_id
WHERE (INNER.QUALIDROOT = INNER.QUALID or INNER.QUALID is NULL)
结果查询应如下所示:
select newKeyword.*, newKeyword.stud_id as **"inner"**, newKeyword . stud_id
from (select NVL(qs.qual_id,qr.qual_id) AS qualIDRoot,qr.stud_id, qr.qual_id as qualID
from pa_stud_qual_relation qr,
pa_qual_subqual qs
where 1 = 1
and qs.qual_id_sub(+)=qr.qual_id
group by qr.stud_id, qr.qual_id, NVL(qs.qual_id,qr.qual_id)
) newKeyword
**inner join** pa_qual_cpnt qc on newKeyword.qualid=qc.qual_id
WHERE (newKeyword.QUALIDROOT = newKeyword.QUALID or newKeyword.QUALID is NULL) .
我尝试了多种不同的正则表达式组合,但无法获得理想的结果。
String test = sql;
Pattern r = Pattern.compile("(\\binner\\b)[^(inner\\s+join)]",
Pattern.CASE_INSENSITIVE); // To combine postive and negative regex
Matcher m = r.matcher(sql);
while (m.find()) {
String str = m.group(0);
System.out.println(str);
test = test.replace(str, "newKeyword");
}
先谢, 阿里
答案 0 :(得分:0)
在我看来,您在这里使事情变得过于复杂。我的意思是,除非您从示例中忽略了一些复杂性,否则您需要做的只是:
query
.replaceAll("INNER","newKeyword") //keyword replace
.replaceAll("inner join","**innej join**") //stars around inner join
.replaceAll("\"inner\"", "**\"inner\"**") //stars around inner in quotes
如果您不能依靠INNER
在newKeyword
情况下总是大写,则可以替换inner.
并在该方面也可以。