1. 程式人生 > >Oracle的REGEXP INSTR函數簡單用法

Oracle的REGEXP INSTR函數簡單用法

語法 style 找到 from source before 執行 def cte

REGEXP_INSTR函數讓你搜索一個正則表達式模式字符串。函數使用輸入字符集定義的字符進行字符串的計算。

它返回一個整數,指示開始或結束匹配的子位置,這取決於return_option參數的值。如果沒有找到匹配,則函數返回0。

語法

Oracle數據庫中的REGEXP_INSTR函數的語法是:

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

參數

source_char

搜索值的字符表達式,可以是任何數據類型CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB或NCLOB的。

pattern

正則表達式

Value Description
^ 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.
\n n 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 匹配任何非空白字符.
\A Matches the beginning of a string or matches at the end of a string before a newline character.
\Z Matches 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功能匹配的行為。它可以是以下的組合:

Value Description
‘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
  • 3
  • 4

下面這個例子給出一個字符串, “1”為開始位置 “2”是搜索第二個匹配的,”0”是return_option 返回出現的第一個字符位置

“c”是區分大小寫 ,所以將返回13

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

-- Result: 13
  • 1
  • 2
  • 3
  • 4

Examples 匹配多個字符

我們將使用REGEXP_INSTR函數來匹配多字符模式。

SELECT REGEXP_INSTR (‘World filled with love‘, ‘with‘, 1, 1, 0, ‘i‘)
FROM dual;
  • 1
  • 2

這個例子將字符串中返回’with’的第一次出現,它將匹配一個詞組。

我們可以改變搜索的開始位置,以便我們執行搜索從字符串的中間開始。

For example:

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

這個例子將開始搜索”my”在字符串中的位置10。在這種情況下,在搜索之前,它會跳過字符串中的前9個字符。

Examples 匹配多個備選

下面的例子,我們將使用 | 模式。該|模式用於像一個”或”指定多個替代方案。

For example:

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

-- Result: 6
  • 1
  • 2
  • 3
  • 4

這個例子將返回6,因為它是搜索的第一個元音(a,i,o,e或u)字符串。由於我們沒有指定match_parameter值時,

REGEXP_INSTR函數將執行區分大小寫的搜索,這意味著在”Itmyhome”的’I’將不匹配。


作者:itmyhome

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Oracle的REGEXP INSTR函數簡單用法