oracle 之replace 和 regexp_replace函数

oracle中replace函数:

用法:replace(tarString,paString,gString)。

参数说明:tarString,要替换的原字符串;paString,需要替换的子字符串;gString,需要替换的目标字符串。

具体说明:

select replace('01111','0','2') from dual;
结果是

21111
若减少gString参数,即为将tarString中为paString的地方删除。

具体说明:

select replace('01111','0') from dual;
结果为

1111

oracle中regexp_replace函数:


转自http://blog.csdn.net/itmyhome1990/article/details/50380718

转自http://blog.csdn.net/itmyhome1990/article/details/50380718

转自http://blog.csdn.net/itmyhome1990/article/details/50380718

重要的事情说三遍。

REGEXP_REPLACE让你搜索的字符串的正则表达式模式REPLACE函数的功能。默认情况下,

该函数返回source_char与replace_string取代了正则表达式模式的每个实例。

返回的字符串是在相同的字符集source_char。

语法

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

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

参数

source_char

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

pattern

正则表达式

Value Description
^ Matches the beginning of a string. If used with amatch_parameterof ‘m’, it matches the start of a line anywhere withinexpression.
$ Matches the end of a string. If used with amatch_parameterof ‘m’, it matches the end of a line anywhere withinexpression.
* 匹配零个或多个.
+ 匹配一个或多个出现.
? 匹配零次或一次出现.
. 匹配任何字符,除了空.
| 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.

replace_string

可选。匹配的模式将被替换replace_string字符串。如果省略replace_string参数,

将删除所有匹配的模式,并返回结果字符串。

position

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

occurrence

可选。是一个非负整数默认为1,指示替换操作的发生:

如果指定0,那么所有出现将被替换字符串。

如果指定了正整数n,那么将替换第n次出现。

match_parameter

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

Value Description
‘c’ 区分大小写的匹配.
‘i’ 不区分大小写的匹配.
‘n’ Allows the period character (.) to match the newline character. By default, the period is a wildcard.
‘m’ expressionis 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 inexpression. By default,expressionis assumed to be a single line.
‘x’ Whitespace characters are ignored. By default, whitespace characters are matched like any other character.

EXAMPLE - 匹配第一个字

让我们开始使用REGEXP_REPLACE函数替换字符串中的第一个字。

For example:

SELECT REGEXP_REPLACE ('itmyhome is my network id', '^(\S*)', 'luck')
FROM dual;

Result: luck is my network id
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这个例子会返回”luck is my network id”,

因为它会再字符串的开始找到第一个匹配的字符,然后替换为”luck”

EXAMPLE - 匹配数字字符

我们将使用REGEXP_REPLACE函数来匹配单个数字字符模式。

For example:

SELECT REGEXP_REPLACE ('2, 5, and 10 are numbers in this example', '\d', '#')
FROM dual;

Result: '#, #, and ## are numbers in this example'
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

此示例将所指定的\d数字将以#字符替换

我们可以改变我们的正则模式来搜索仅两位数字。

For example:

SELECT REGEXP_REPLACE ('2, 5, and 10 are numbers in this example', '(\d)(\d)', '#')
FROM dual;

Result: '2, 5, and # are numbers in this example'
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这个例子将替换具有两个数字并排指定的(\d)(\d)模式。在这种情况下,它将跳过2和5个数字值和用#字符替换10。



还有一个问题:就是同replace,只有前两个参数是什么意思

具体说明

select regexp_replace('01111','^0') from dual;

结果是

1111
含义同replace函数。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值