2010-07-15,07-19 1Z0-047中关于正则表达式的两道题

(一)

92.Evaluate the following expression using meta character for regular expression:

'[^Ale|ax.r$]'

Which two matches would be returned by this expression? (Choose two.)

A. Alex

B. Alax

C. Alxer

D. Alaxendar

E. Alexender

[@more@]这道题考察对正则表达式运算符 [...] 的理解,答案是 AD 。《Oracle Database 11g SQL Fundamentals II》的教材中对于 [...] 的解释为“Matches any single character in the list within the brackets”,也就是说 [...] 匹配的是一个字符,而不是一堆字符。见如下实验:

SQL> select * from dual where regexp_like('b', '^[abc]$');

D
-
X

SQL> select * from dual where regexp_like('abc', '^[abc]$');

no rows selected

SQL> select * from dual where regexp_like('abc', '^a[abc]c$');

D
-
X


题目中没有使用 ^ 和 $ 或者其它字符限定位置, 所以 [^Ale|ax.r$] 只要匹配源字符串中的任意位置的一个字符就可以了。现在再来分析中括号里面的内容, ^ 表示对后面的内容取反,而 | . $ 在中括号里时不再是运算符,只是普通的字符。见如下实验:

SQL> select * from dual where regexp_like('|', '[|]');

D
-
X

SQL> select * from dual where regexp_like('.', '[.]');

D
-
X

SQL> select * from dual where regexp_like('a', '[.]');

no rows selected

SQL> select * from dual where regexp_like('$', '[$]');

D
-
X

SQL> select * from dual where regexp_like('abc', '[a$]');

D
-
X

SQL> select * from dual where regexp_like('abc', 'a$');

no rows selected


所以, '[^Ale|ax.r$]' 匹配的是在任意位置上拥有除 A l e | a x . r $ 以外的其它字符的字符串。 D 和 E 中的两个字符串都包含 n 和 d ,是正确答案。


(二)

108. View the Exhibit and examine the details for the CATEGORIES_TAB table.

Evaluate the following incomplete SQL statement:

SELECT category_name,category_description

FROM categories_tab

You want to display only the rows that have 'harddisks' as part of the string in the CATEGORY_DESCRIPTION column.

Which two WHERE clause options can give you the desired result? (Choose two.)

A. WHERE REGEXP_LIKE (category_description, 'hard+.s');

B. WHERE REGEXP_LIKE (category_description, '^H|hard+.s');

C. WHERE REGEXP_LIKE (category_description, '^H|hard+.s$');

D. WHERE REGEXP_LIKE (category_description, '[^H|hard+.s]');

这里不附图了,CATEGORIES_TAB中只有一条记录的 category_description 字段的值为 harddisks ,其它记录该字段的值都不包含这一字符串。这道题考察REGEXP_LIKE的使用,正确答案是 AB 。从选项A看,很容易让人以为使用的模式 'hard+.s' 是在匹配 harddisks ,其实它匹配的只是一部分 harddis 。其中 + 表示前面的字母 d 出现一或多次,匹配了 dd ;而 . 表示任意一个字符,匹配了 i 。见下面一组实验:

SQL> select * from dual where regexp_like('harddisks', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('harddis', 'hard+.s');

D
-
X


没有第二个 d ,或者再多加几个 d 都是正确的:

SQL> select * from dual where regexp_like('hardis', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('harddddis', 'hard+.s');

D
-
X


字母 i 的位置可以是任意一个字符:

SQL> select * from dual where regexp_like('harddms', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('hardd7s', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('hardd!s', 'hard+.s');

D
-
X


使用的模式只要匹配了源字符串中的一部分,regexp_like就返回TRUE,例如:

SQL> select * from dual where regexp_like('1a@harddis', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('harddis2b#', 'hard+.s');

D
-
X

SQL> select * from dual where regexp_like('3c+harddis4d-', 'hard+.s');

D
-
X


选项 B 和 C 中的 ^ 和 $ ,分别表示字符串的头部和尾部,所以 C 是错误的,因为在 harddis 后面还有 ks 呢。而 D 中的中括号表示括号里是一个匹配列表,这个时候 ^ 不再表示字符串的头部,而是对后面的内容取反,因此也是错误的。另外,注意 B 中的 '^H|hard+.s' 匹配的是以 H 开头或者满足 hard+.s 的字符串,例如 Hawk 也是符合条件的;而不是以 H 或 h 开头,后面满足 ard+.s 的字符串,那样应该写作 '^(H|h)ard+.s' 。
这道题的难度并不算高,但会让人产生错觉,认为模式 'hard+.s' 是在匹配整个字符串 harddisks 。想不到老外出题也会玩阴的,难道是和中国人学的?让我不禁想起了高考前做38套题,封面上写着“博览神州考卷精华”,妈的,万恶的高考!跑题了,哈 ^o^

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11662464/viewspace-1035324/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11662464/viewspace-1035324/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值