您的位置:首页 > 数据库 > Oracle

Oracle的REGEXP_INSTR函数简单用法

2015-12-22 22:46 495 查看
REGEXP_INSTR函数让你搜索一个正则表达式模式字符串。函数使用输入字符集定义的字符进行字符串的计算。

它返回一个整数,指示开始或结束匹配的子位置,这取决于return_option参数的值。如果没有找到匹配,则函数返回0。

语法

Oracle数据库中的REGEXP_INSTR函数的语法是:

REGEXP_INSTR (source_char, pattern [, position [, occurrence [, return_option [, match_parameter ] ] ]  ] )


参数

source_char

搜索值的字符表达式,可以是任何数据类型CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB或NCLOB的。

pattern

正则表达式

ValueDescription
^Matches the beginning of a string. If used with a match_parameter of ‘m’, it matches the start of a line anywhere within expression.
$Matches the end of a string. If used with a match_parameter of ‘m’, it matches the end of a line anywhere within expression.
*匹配零个或多个.
+匹配一个或多个出现.
?匹配零次或一次出现.
.匹配任何字符,除了空.
|Used like an "OR" to specify more than one alternative.
[ ]Used to specify a matching list where you are trying to match any one of the characters in the list.
[^ ]Used to specify a nonmatching list where you are trying to match any character except for the ones in the list.
( )Used to group expressions as a subexpression.
{m}Matches m times.
{m,}Matches at least m times.
{m,n}Matches at least m times, but no more than n times.
\nn is a number between 1 and 9. Matches the nth subexpression found within ( ) before encountering \n.
[..]Matches one collation element that can be more than one character.
[::]Matches character classes.
[==]Matches equivalence classes.
\d匹配一个数字字符.
\D匹配一个非数字字符.
\w匹配包括下划线的任何单词字符.
\W匹配任何非单词字符.
\s匹配任何空白字符,包括空格、制表符、换页符等等.
\S匹配任何非空白字符.
\AMatches the beginning of a string or matches at the end of a string before a newline character.
\ZMatches at the end of a string.
*?Matches the preceding pattern zero or more occurrences.
+?Matches the preceding pattern one or more occurrences.
??Matches the preceding pattern zero or one occurrence.
{n}?Matches the preceding pattern n times.
{n,}?Matches the preceding pattern at least n times.
{n,m}?Matches the preceding pattern at least n times, but not more than m times.

position

可选。搜索在字符串中的开始位置。如果省略,则默认为1,这是字符串中的第一个位置。

occurrence

可选。它是模式字符串中的第n个匹配位置。如果省略,默认为1。

return_option

可选 指定Oracle返回的位置:

如果指定0,那么Oracle将返回出现的第一个字符的位置。这是默认的。

如果指定1,则Oracle返回字符之后发生的位置。

match_parameter

可选。它允许你修改REGEXP_INSTR功能匹配的行为。它可以是以下的组合:

ValueDescription
‘c’区分大小写的匹配.
‘i’不区分大小写的匹配.
‘n’Allows the period character (.) to match the newline character. By default, the period is a wildcard.
‘m’expression is assumed to have multiple lines, where ^ is the start of a line and $ is the end of a line, regardless of the position of those characters in expression. By default, expression is assumed to be a single line.
‘x’Whitespace characters are ignored. By default, whitespace characters are matched like any other character.

Examples 匹配单个字符

下面看一个最简单的情况,找到字符串中的第一个”e”字的位置。

SELECT REGEXP_INSTR ('hello itmyhome', 'e')
FROM dual;

-- Result: 2


下面这个例子给出一个字符串, “1”为开始位置 “2”是搜索第二个匹配的,”0”是return_option 返回出现的第一个字符位置

“c”是区分大小写 ,所以将返回13

SELECT REGEXP_INSTR ('my is itMyhome', 'm', 1, 2, 0, 'c')
FROM dual;

-- Result: 13


Examples 匹配多个字符

我们将使用REGEXP_INSTR函数来匹配多字符模式。

SELECT REGEXP_INSTR ('World filled with love', 'with', 1, 1, 0, 'i')
FROM dual;


这个例子将字符串中返回’with’的第一次出现,它将匹配一个词组。

我们可以改变搜索的开始位置,以便我们执行搜索从字符串的中间开始。

For example:

SELECT REGEXP_INSTR ('my name is itmyhome', 'my', 10, 1, 0, 'i')
FROM dual;


这个例子将开始搜索”my”在字符串中的位置10。在这种情况下,在搜索之前,它会跳过字符串中的前9个字符。

Examples 匹配多个备选

下面的例子,我们将使用 | 模式。该|模式用于像一个”或”指定多个替代方案。

For example:

SELECT REGEXP_INSTR ('Itmyhome', 'a|i|o|e|u')
FROM dual;

-- Result: 6


这个例子将返回6,因为它是搜索的第一个元音(a,i,o,e或u)字符串。由于我们没有指定match_parameter值时,

REGEXP_INSTR函数将执行区分大小写的搜索,这意味着在”Itmyhome”的’I’将不匹配。

作者:itmyhome
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: