Java 提取两个字符串之间的字符串,其中第二个字符串可能存在,也可能不存在
我想用java解析一个sql查询。这是我项目的一部分。 现在我需要提取'from'子句中的参数。因为它们存在于“from”和“where”子句之间,所以我使用以下模式进行匹配Java 提取两个字符串之间的字符串,其中第二个字符串可能存在,也可能不存在,java,mysql,regex,string,Java,Mysql,Regex,String,我想用java解析一个sql查询。这是我项目的一部分。 现在我需要提取'from'子句中的参数。因为它们存在于“from”和“where”子句之间,所以我使用以下模式进行匹配 Pattern p = Pattern.compile("from" + "(.*?)" + "(where)?"); 因为where子句可能存在于查询中,也可能不存在于查询中,所以我在where后面使用了“?”。但是当我使用像这样的查询时,我没有得到预期的“from”参数 select * from
Pattern p = Pattern.compile("from" + "(.*?)" + "(where)?");
因为where子句可能存在于查询中,也可能不存在于查询中,所以我在where后面使用了“?”。但是当我使用像这样的查询时,我没有得到预期的“from”参数
select * from student;
我对正则表达式不是很熟悉。请帮助提示:为什么不只是
子字符串
它?
/*
Regex explanation:
'from' - when found `from` char sequence
'\\s*' - skip whitespace
'(.*?)' - capture some chars in `non-greedy` mode
'(' - and then
'where' - must be `where` char sequence
'|;' - or a semicolon
'|$' - or end of matching string
')'
*/
static Pattern p = Pattern.compile("from\\s*(.*?)(where|;|$)");
static String extract1(String query) {
String tables = null;
Matcher m = p.matcher(query);
if (m.find())
tables = m.group(1).trim();
return tables;
}
static String extract2(String query) {
String term = query.toLowerCase();
int from = term.indexOf("from");
if (from < 0)
return null;
int to = term.indexOf("where");
if (to < 0) {
to = term.length();
while (term.charAt(to - 1) == ';')
to--;
}
return query.substring(from + 4, to).trim();
}
public static void main(String[] args) {
String query1 = "select * from table as t";
String query2 = "select * from table as t;";
String query3 = "select * from table as u where a = b";
String query4 = "select * from table as u where a = b;";
String t1, t2;
t1 = extract1(query1);
t2 = extract2(query1);
System.out.printf("\"%s\":\n %s\n %s\n\n", query1, t1, t2);
t1 = extract1(query2);
t2 = extract2(query2);
System.out.printf("\"%s\":\n %s\n %s\n\n", query2, t1, t2);
t1 = extract1(query3);
t2 = extract2(query3);
System.out.printf("\"%s\":\n %s\n %s\n\n", query3, t1, t2);
t1 = extract1(query4);
t2 = extract2(query4);
System.out.printf("\"%s\":\n %s\n %s\n\n", query4, t1, t2);
}
请尝试以下两种方法之一:
\\b从([^w]*(?:w(?!here\\b)[^w]*)
或(?s)\\b从((?:(?!\\bwhere\\b)。*)
@stribizhev感谢它的工作。我从((?:(?!\\b此处\\b)使用了这个(?s)\\b.*)
。。但它会和分号一起提取为“student;”。如何忽略分号,请告诉我这个正则表达式是如何工作的,这些字符表示什么?你不认为下面的答案就是你需要的吗?如果,你可以使用代码>始终位于末尾,因为您需要的值位于组1中。(?:(?!\\b此处\\b)。*
构造称为令牌。这个解决方案不是很有效,所以只有当你不能使用其他任何东西时才使用它。哇,谢谢。这种子字符串方法非常有用。谢谢@ankhzetI有点困惑:“return query.substring(from+4,to).trim();”中的“4”来自哪里?term.indexOf(“from”)
返回第一个位置,即from
子字符串的起始位置。但我们只希望查询的部分位于之后,即from
关键字,所以在检索子字符串(to+4,…)
时跳过它,否则将从表中得到作为u
4
只是从
部分跳过的()长度。
"select * from table as t":
table as t
table as t
"select * from table as t;":
table as t
table as t
"select * from table as u where a = b":
table as u
table as u
"select * from table as u where a = b;":
table as u
table as u