Oracle正则表达式-不以且不以 [英] Oracle regex - does not start with and does not end with

查看:512
本文介绍了Oracle正则表达式-不以且不以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下Oracle正则表达式不起作用,我也不知道为什么.

The following Oracle regular expressions do not work and I don't know why.

不以' abc '开头":

^[^(abc)]

不以' abc '结尾"

[^(abc)]$

问题是Oracle正则表达式引擎似乎无法将'abc'字符串识别为一个单元,而只是单独查看字母.括号()应该创建一个字符串单元.所以我不知道发生了什么.我之所以使用方括号,是因为我相信'not'运算符^只能在方括号内进行运算,否则^被识别为行首.

The problem is that the Oracle regex engine does not seem to recognize the 'abc' string as a unit, but only is looking at the letters individually. The parentheses () are supposed to create a string unit. So I don't know what is going on. I used the square brackets only because I believe the 'not' operator ^ only operates inside the brackets, otherwise the ^ is recognized as start of line.

供参考: http://docs.oracle.com/cd/B12037_01/appdev .101/b10795/adfns_re.htm

推荐答案

像这样的不匹配测试可能会变得很复杂,因此,我建议测试匹配并否定结果.

Testing for non-matching like this can get complicated, so I'd recommend testing for a match and negating the result.

不是以abc开头:

WHERE NOT REGEXP_LIKE(myString, '^abc')

不以abc结尾:

WHERE NOT REGEXP_LIKE(myString, 'abc$')


至于为什么它不起作用,正如@DavidKnipe在他的回答中所说:这是因为您正在使用字符类.正则表达式^[^(abc)]解析如下:


As for why it doesn't work, as @DavidKnipe says in his answer: it's because you're using character classes. The regex ^[^(abc)] parses out like this:

  • 第一个^说锚定到字符串的开头"
  • [^(abc)]是一个字符类,它说匹配任何单个字符,只要它不是(abc)"
  • The first ^ says "anchor to the beginning of the string"
  • The [^(abc)] is a character class that says "match any single character as long as it's not ( or a or b or c or )".

这篇关于Oracle正则表达式-不以且不以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆