Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 提取两个字符串之间的字符串,其中第二个字符串可能存在,也可能不存在_Java_Mysql_Regex_String - Fatal编程技术网

Java 提取两个字符串之间的字符串,其中第二个字符串可能存在,也可能不存在

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

我想用java解析一个sql查询。这是我项目的一部分。 现在我需要提取'from'子句中的参数。因为它们存在于“from”和“where”子句之间,所以我使用以下模式进行匹配

     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