正则表达式不适用于行的开头或结尾

时间:2013-02-05 17:06:57

标签: regex oracle matching

我尝试了[^ A-Za-z0-9] 帐篷[^ A-Za-z0-9] ..有多种组合?+ *

以及类似[^ | [^ A-Za-z0-9] ]帐篷[[^ A-Za-z0-9] | $]

我想要匹配:

  • 关注 - 否
  • 看看帐篷 - 是
  • 帐篷很不错 - 是的
  • 帐篷的东西 - 是的
  • 露营,帐篷,户外 - 是(有些 非字母数字分隔符)

如果它更容易像“帐篷”这样的东西也可以匹配

我想我在所看到的所有页面上都遗漏了一些东西,但我真的被困在这里了。我需要在句子的开头,中间或结尾或分隔列表中找到一个搜索词。我需要做多个陈述吗?

我希望有一种方法可以说使用这个表达式但不是如果它是行的开头或行的结尾

1 个答案:

答案 0 :(得分:3)

使用\w(非字母数字)

SQL> select str
  2    from (select 'attention' str from dual union all
  3          select 'look at the tent' str from dual union all
  4          select 'tent''s are nice' str from dual union all
  5          select 'something tent something' str from dual union all
  6          select 'camping,tent,outdoors' str from dual)
  7   where REGEXP_LIKE(str, '(\W|^)tent(\W|$)', 'i') ;

STR
------------------------
look at the tent
tent's are nice
something tent something
camping,tent,outdoors

SQL> select str,
  2         case when REGEXP_LIKE(str, '(\W|^)tent(\W|$)', 'i') then 'YES' else 'NO' end matches
  3    from (select 'attention' str from dual union all
  4          select 'look at the tent' str from dual union all
  5          select 'tent''s are nice' str from dual union all
  6          select 'something tent something' str from dual union all
  7          select 'tent' str from dual union all
  8          select 'camping,tent,outdoors' str from dual);

STR                      MAT
------------------------ ---
attention                NO
look at the tent         YES
tent's are nice          YES
something tent something YES
tent                     YES
camping,tent,outdoors    YES