参考网址:http://www.oradev.com/regular_expressions_sql_oracle.jsp
Metacharacters | Meaning | Examples |
\ | Indicates that the match character is a special character, a literal, or a backreference. (A backreference repeats the previous match.) |
相当于转义符 \n matches the newline character,\\ matches \, \( matches (, \) matches ) |
^ | Matches the position at the start of the string. |
^A matches A if A is the first character in the string. 匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。 |
$ | Matches the position at the end of the string. |
$B matches B if B is the last character in the string. 匹配输入字符串的结尾位置。 |
* | Matches the preceding character zero or more times. | ba*rk matches brk, bark, baark, and so on.匹配前面的字符0次,1次,多次 |
+ | Matches the preceding character one or more times. |
ba+rk matches bark, baark, and so on, but not brk. 匹配前面的字符1次,多次 |
? | Matches the preceding character zero or one time. | ba?rk matches brk and bark only. 匹配前面的字符0次,1次 |
{n} | Matches a character exactly n times, where n is an integer. |
hob{2}it matches hobbit.(加上自己总共两次) 一个精确地出现次数范围,m=<出现次数<=n,'{m}'表示出现m次,'{m,}'表示至少出现m次。 |
{n,m} | Matches a character at least n times and at most m times, where n and m are both integers. | hob{2,3}it matches hobbit and hobbbit only. |
. | Matches any single character except null. | hob.it matches hobait, hobbit, and so on.(匹配任何单字符) |
(pattern) | A subexpression that matches the specified pattern. You use subexpressions to build up complex regular expressions. You can access the individual matches, known as captures, from this type of subexpression. | anatom(y|ies) matches anatomy and anatomies. |
x|y | Matches x or y, where x and y are one or more characters. | war|peace matches war or peace.(两者择一) |
[abc] | Matches any of the enclosed characters. | [ab]bc matches abc and bbc.(匹配方括号内的字符) |
[a-z] | Matches any character in the specified range. | [a-c]bc matches abc, bbc, and cbc.(匹配范围内的字符) |
[: :] | Specifies a character class and matches any character in that class. | [:alphanum:] matches alphanumeric characters 0-9, A-Z, and a-z. [:alpha:] matches alphabetic characters A-Z and a-z. [:blank:] matches space or tab. [:digit:] matches digits 0-9.[:graph:] matches non-blank characters.[:lower:] matches lowercase alphabetic characters a-z.[:print:] is similar to [:graph:] except [:print:] includes the space character.[:punct:] matches punctuation characters .,"', and so on.[:space:] matches all whitespace characters.[:upper:] matches all uppercase alphabetic characters A-Z.[:xdigit:] matches characters permissible in a hexadecimal number 0-9, A-F, and a-f. |
[..] | Matches one collation element, like a multicharacter element. | No example. |
[==] | Specifies equivalence classes. | No example. |
\n | This is a backreference to an earlier capture, where n is a positive integer. | (.)\1 matches two consecutive identical characters. The (.) captures any single character except null, and the \1 repeats the capture, matching the same character again, therefore matching two consecutive identical characters. |
参考网址:http://zhouwf0726.itpub.net/post/9689/189020
racle的正则表达式(regular expression)简单介绍
目前,正则表达式已经在很多软件中得到广泛的应用,包括*nix(Linux, Unix等),HP等操作系统,PHP,C#,Java等开发环境。
Oracle 10g正则表达式提高了SQL灵活性。有效的解决了数据有效性, 重复词的辨认, 无关的空白检测,或者分解多个正则组成
的字符串等问题。
Oracle 10g支持正则表达式的四个新函数分别是:REGEXP_LIKE、REGEXP_INSTR、REGEXP_SUBSTR、和REGEXP_REPLACE。
它们使用POSIX 正则表达式代替了老的百分号(%)和通配符(_)字符。
特殊字符:
'^' 匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。
'$' 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 'n' 或 'r'。
'.' 匹配除换行符 n之外的任何单字符。
'?' 匹配前面的子表达式零次或一次。
'*' 匹配前面的子表达式零次或多次。
'+' 匹配前面的子表达式一次或多次。
'( )' 标记一个子表达式的开始和结束位置。
'[]' 标记一个中括号表达式。
'{m,n}' 一个精确地出现次数范围,m=<出现次数<=n,'{m}'表示出现m次,'{m,}'表示至少出现m次。
'|' 指明两项之间的一个选择。例子'^([a-z]+|[0-9]+)$'表示所有小写字母或数字组合成的字符串。
num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。
正则表达式的一个很有用的特点是可以保存子表达式以后使用, 被称为Backreferencing. 允许复杂的替换能力
如调整一个模式到新的位置或者指示被代替的字符或者单词的位置. 被匹配的子表达式存储在临时缓冲区中,
缓冲区从左到右编号, 通过数字符号访问。 下面的例子列出了把名字 aa bb cc 变成
cc, bb, aa.
Select REGEXP_REPLACE('aa bb cc','(.*) (.*) (.*)', '3, 2, 1') FROM dual;
REGEXP_REPLACE('ELLENHILDISMIT
cc, bb, aa
'' 转义符。
字符簇:
[[:alpha:]] 任何字母。
[[:digit:]] 任何数字。
[[:alnum:]] 任何字母和数字。
[[:space:]] 任何白字符。
[[:upper:]] 任何大写字母。
[[:lower:]] 任何小写字母。
[[:punct:]] 任何标点符号。
[[:xdigit:]] 任何16进制的数字,相当于[0-9a-fA-F]。
各种操作符的运算优先级
转义符
(), (?:), (?=), [] 圆括号和方括号
*, +, ?, {n}, {n,}, {n,m} 限定符
^, $, anymetacharacter 位置和顺序
| “或”操作
正则表达式方法:
The regular expression functions available in Oracle Database 10g.
REGEXP_LIKE(x, pattern [, match_option])
Returns true when the source x matches the regular expression pattern.
You can change the default matching using match_option, which may be set to:
'c', which specifies case sensitive matching (default).默认大小写敏感
'i', which specifies case insensitive matching.大小写不敏感
'n', which allows you to use the match-any-character operator.
'm', which treats x as multiple line.
REGEXP_INSTR(x, pattern [, start [, occurrence [, return_option [, match_option]]]])
Searches for pattern in x and returns the position at which pattern occurs.
You can supply an optional:
start position to begin the search.--开始位置
occurrence that indicates which occurrence of pattern_exp should be returned.--那个位置出现的值应该被返回
return_option that indicates what integer to return. 0 specifies the integer to return is the position of the first character in x; non-zero specifies the integer to return is the position of the character in x after the occurrence.--0表示出现第一个字符的位置。1表示匹配表达式出现后的位置
match_option to change the default matching.--类似与REGEXP_LIKE的表
REGEXP_REPLACE(x, pattern [, replace_string [, start [, occurrence [, match_option]]]])
Searches x for pattern and replaces it with replace_string.
The other options have the same meaning as those shown earlier.
REGEXP_SUBSTR(x, pattern [, start [, occurrence [, match_option]]])
Returns a substring of x that matches pattern, which begins at the position specified by start.
The other options have the same meaning as those shown earlier.
REGEXP_INSTR的使用:
REGEXP_INSTR extends the regular INSTR string function by allowing searches of regular expressions.
The simplest form of this function is:
REGEXP_INSTR(source_string, pattern_to_find)
This part works like the INSTR function.
The general format for the REGEXP_INSTR function with all the options is:
1、source_string is the string in which you wish to search for the pattern.
2、pattern_to_find is the pattern that you wish to search for in a string.
3、position indicates where to start searching in source_string.
4、occurrence indicates which occurrence of the pattern_to_find (in the source_string) you wish to search for.
5、For example, which occurrence of "si" do you want to extract from the source string "Mississippi".
6、return_option can be 0 or 1.
7、If return_option is 0, Oracle returns the first character of the occurrence (this is the default);
8、if return_option is 1, Oracle returns the position of the character following the occurrence.
9、match_parameter allows you to further customize your search.
10、"i" in match_parameter can be used for caseinsensitive matching
11、"c" in match_parameter can be used for casesensitive matching
12、"n" in match_parameter allows the period to match the new line character
13、"m" in match_parameter allows for more than one line in source_string
1 SELECT REGEXP_INSTR('Mississippi', 'si', 1,2,0,'i') FROM dual;
结果:7
i表示大小写敏感。
1 SELECT REGEXP_INSTR('Mississippi', 'si', 1,2,1,'i') FROM dual;
结果:9
1表示返回找到的字符串的下个字符开始位置。
1 SELECT REGEXP_INSTR('abcedfghijklumnoprstuvwxyzabcedhijklumnoprstuvwxyz',
2 'l[[:alpha:]]{4}') AS RESULT
3 FROM DUAL;
结果:12
表示查询从1开始的后面任意有四个字符的位置
REGEXP_LIKE的使用:
REGEXP_LIKE(x, pattern [, match_option]) searchs x for the regular expression pattern.
You can provide an optional match_option string to indicate the default matching.
'c', which specifies case sensitive matching (default).大小写敏感
'i', which specifies case insensitive matching.大小写不敏感
'n', which allows you to use the match-any-character operator.
'm', which treats x as multiple line.
1 WITH DATAS AS
2 (SELECT 'Hello' AS D
3 FROM DUAL
4 UNION ALL
5 SELECT 'hello' FROM DUAL)
6 SELECT * FROM DATAS WHERE REGEXP_LIKE(DATAS.D, '^h', 'i')
结果:
1 WITH DATAS AS
2 (SELECT 'a' AS D
3 FROM DUAL
4 UNION ALL
5 SELECT 'b' FROM DUAL)
6 SELECT * FROM DATAS WHERE REGEXP_LIKE(DATAS.D, '[ab]', 'i')
结果:
a
b