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

Oracle的REGEXP SUBSTR函式簡單用法

REGEXP_SUBSTR延伸SUBSTR函式的功能,讓你搜索一個正則表示式模式字串。

這也類似於REGEXP_INSTR,而是返回子字串的位置,它返回的子字串本身。

語法

Oracle資料庫中的REGEXP_SUBSTR函式的語法是:

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

引數

source_char

搜尋字串。可以是任意的資料型別char,VARCHAR2,nchar,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。

match_parameter

可選。它允許你修改regexp_substr功能匹配的行為。它可以是以下的組合:

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

找出匹配的數字

SELECT REGEXP_SUBSTR ('hello my phone is 520 ', '[0-9]+') FROM dual; --520
  • 1

下面這個例子返回指定第三次出現的字元

SELECT REGEXP_SUBSTR ('i like beijing tiananmen', '(\S*)(\s)', 1, 3)
FROM dual;    --beijing
  • 1
  • 2


作者:itmyhome

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