代码之家  ›  专栏  ›  技术社区  ›  Adnan

Oracle中的简单正则表达式

  •  2
  • Adnan  · 技术社区  · 15 年前

    ID     NAME        ADDRESS
    1      Bob         Super stree1 here goes
    2      Alice       stree100 here goes
    3      Clark       Fast left stree1005
    4      Magie       Right stree1580 here goes
    

    我需要使用 LIKE 只有一排 stree1 (在本例中,只获取ID=1的那个),我使用以下查询:

    select * from table t1 WHERE t1.ADDRESS LIKE '%stree1%';
    

    但问题是我得到的所有行都包含 街道1 加上后面的字符/数字。

    我发现我可以用 REGEXP_LIKE 由于我使用的是oracle,在以下情况下应该使用什么样的正则表达式:

    select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1');
    
    3 回复  |  直到 10 年前
        1
  •  4
  •   Michael Goldshteyn    15 年前

    我想这就是你要找的前注册律师:

    select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1(?:[^[:word:]]|$)');
    

    如果需要,可以进一步简化为:

    select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1(?:\W|$)');
    

    也就是说,“stree1”后面没有单词字符(即,后面跟着空格/标点符号等),或者“stree1”出现在字符串的末尾。当然,还有很多其他方法可以做到这一点,包括单词边界'stree1\b',在stree1中的1后面需要特定的字符(例如,带有'stree1\s'的空格),等等。。。

        2
  •  0
  •   tinifni    15 年前

    这可能有助于:

    stree1\b
    
        3
  •  0
  •   kleopatra Aji kattacherry    13 年前

    第一个'\W'是告诉它一个非单词字符,因为您需要在'stree1'后面加上注释,但是要用空格 “$”告诉如果以stree1结尾,则将其作为有效字符串

    select *
    from table1
    where regexp_like(address,'stree1(\W|$)')