繁体   English   中英

如何替换此字符串中的所有值?

[英]How can I replace all values in this string?

我在具有不同“大小”值的表的列中有一个字符串。 例如:

select 'Test size="7" size="14" text size="14" line="22" other size="10"  size="9" '
from dual;

如何更改字符串中所有大小的值,而不替换字符串中的其他数字?

对于所有实例,要替换的值将相同。 例如替换为8:

select 'Test size="8" size="8" text size="8" line="22" other size="8"  size="8" '
from dual;

我尝试过,但是没有生效。 有任何想法吗?:

select regexp_replace('Test size="7" size="14" text size="14" line="22" other size="10"  size="9" ', '/size="[0-9]+"/g', 'size="8"')
from dual

如果要将所有出现的次数都更改为相同的数字,则将REGEXP_REPLACE()与相应的参数一起使用将是最简单的。 第四个参数是您要替换的字符串的出现; 0表示所有出现。 您刚刚添加的正则表达式无法正常工作的原因是因为您没有使用此参数。

with s as (
select 'Test size="7" size="14" text size="14" line="22" other size="10"  size="9" ' as str
  from dual
       )
select regexp_replace(str, 'size="\d+"', 'size="8"', 1, 0)
  from s

正则表达式查找字符串size=" ,后跟一个或多个数字\\d+后跟双引号" 然后,将其替换为包含新数字的字符串。

SQL> WITH DATA AS
  2    (SELECT 'Test size="7" size="14" text size="14" line="22" other size="10"  size="9" ' STR
  3    FROM DUAL
  4    )
  5  SELECT REGEXP_REPLACE(STR, 'size="[[:digit:]]+"','size="8"') STR
  6  FROM DATA
  7  /

STR
------------------------------------------------------------------------
Test size="8" size="8" text size="8" line="22" other size="8"  size="8"

SQL>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM