【问题标题】:H2 not recognising regexp_likeH2 无法识别 regexp_like
【发布时间】:2014-08-31 21:30:37
【问题描述】:

我编写了一个在 Oracle 数据库上运行的查询,该数据库使用函数 REGEXP_LIKE 从查询中过滤某些行。具体的函数调用是

regexp_like(col1, '[^[:alpha:]]')

问题是当我在 H2 上运行查询时出现以下错误:

org.h2.jdbc.JdbcSQLException: Function "REGEXP_LIKE" not found

如果我使用 SQLDeveloper 工具直接在 Oracle 数据库上运行查询,它会按预期返回。

有什么可能导致这种情况的想法吗?

【问题讨论】:

  • 问题是你需要查询同时在Oracle和H2上运行吗?

标签: java regex database jdbc h2


【解决方案1】:

请参阅excellent documentation

col REGEXP '[^[:alpha:]]'

一般的 SQL 变体要么使用函数,要么使用命名运算符。

我不知道上述特定的正则表达式是否有效。应该能够依赖 java 正则表达式。

【讨论】:

    【解决方案2】:

    H2 没有名为regexp_like 的函数。但是您可以使用user defined function 创建一个:

    create alias regexp_like as 
    $$ boolean regexpLike(String s, String p) { return s.matches(p); } $$;
    
    drop table test;
    create table test(id int, name varchar(2555));
    insert into test values(1, 'Steven');
    insert into test values(2, 'Stefan');
    select * from test where regexp_like(name, '^Ste(v|ph)en$');
    

    【讨论】:

      【解决方案3】:

      这是 Thomas Mueller 对 H2 用户函数的略微改进版本,它支持标志和 NULL 值:

      create alias regexp_like as
      $$ boolean regexpLike(String s, String p, String flags) {
          if(null == s) return false;
          if(null != flags) { p = "(?" + flags + ")" + p; }
          java.util.regex.Pattern compiled = java.util.regex.Pattern.compile(p);
          return compiled.matcher(s).find();
      } $$
      

      【讨论】:

      • flags 参数似乎是多余的。调用者可以直接在模式中传递标志。
      • @Stephan:Oracle 版本有三个参数,这就是我需要它的原因。
      【解决方案4】:

      【讨论】:

      • 在 1.4.200 版本中检查:[90022] 未找到 REGEXP_LIKE
      • 它仍然存在于文档和代码中。如果对您不起作用,请提出您自己的问题,并提供详细信息。
      【解决方案5】:

      https://h2database.com/html/grammar.html#regexp_predicate_right_hand_side

         使用正则表达式匹配。有关详细信息,请参阅 Java Matcher.find。示例:REGEXP '[a-z]'

      https://overcoder.net/q/8528/%D1%80%D0%B0%D0%B7%D0%BD%D0%B8%D1%86%D0%B0-%D0%BC%D0%B5%D0%B6%D0%B4%D1%83-match-%D0%B8-find-%D0%B2-java-regex

         如果整个字符串与给定模式匹配,则 Matches 返回 true。 find 尝试查找与模式匹配的子字符串

         匹配 (p) 与 find ("^" + p + "$") 相同

      样本:

      DELETE FROM sampledb.user_t WHERE email REGEXP '^[a-zA-Zа-яА-Я0-9 .-]+$';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        相关资源
        最近更新 更多