The replacement happens because you are only checking for [^a-z0-9]
after the search term, and D
is not in that character class.
(发生替换是因为您仅在搜索词之后检查[^a-z0-9]
,而D
不在该字符类中。)
You can resolve this by either adding AZ
to your character class: (您可以通过将AZ
添加到角色类来解决此问题:)
SELECT REGEXP_REPLACE (UPPER('BIG CATDOG'), '(^|[^a-zA-Z0-9])' || UPPER('CAT') || '($|[^a-zA-Z0-9])', '1' || UPPER('GATO') || '2','g')
Or by adding the i
flag to the replace call:
(或通过将i
标志添加到replace调用中:)
SELECT REGEXP_REPLACE (UPPER('BIG CATDOG'), '(^|[^a-z0-9])' || UPPER('CAT') || '($|[^a-z0-9])', '1' || UPPER('GATO') || '2','gi')
In either case you will get the desired BIG CATDOG
output.
(无论哪种情况,您都将获得所需的BIG CATDOG
输出。)
However a better solution is to use the word boundary constraints \m
(beginning of word) and \M
(end of word):
(但是,更好的解决方案是使用单词边界约束\m
(单词的开头)和\M
(单词的结尾):)
SELECT REGEXP_REPLACE (UPPER('BIG CATDOG'), 'm' || UPPER('CAT') || 'M', UPPER('GATO'),'g')
Demo on dbfiddle
(dbfiddle上的演示)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…