[英]Oracle SQL REGEXP_REPLACE - Everything Except Specified String
I am having an issue with something that I thought would have been simple, but can't for the life of me get it sorted out.我遇到了一些我认为很简单的问题,但我一生都无法解决。 I'm trying to replace everything except a specified string with a blank space.
我正在尝试用空格替换除指定字符串之外的所有内容。
I am able to remove the string (see below), but am unable to 'inverse' the operation.我能够删除字符串(见下文),但无法“反转”操作。
with text as (
select 'Cat dog sheep /* 67 = 123 + monkey12 abcd and then a fish B1234.CAT_DOG_MOUSE and half a loaf of bread /* ON INNER JOIN B3456.BIRD_SHOE 11' as jibberish)
select regexp_replace(jibberish, '(B)[[:digit:]]{4}[.][[:alnum:]_]+', ' ') as new_text from text;
'Cat dog sheep /* 67 = 123 + monkey12 abcd and then a fish and half a loaf of bread /* ON INNER JOIN 11'
I have tried various things, such as '[^(B)[[:digit:]]{4}[.][[:alnum:]_]+]*' and various other things that I've found on here, but none of them seem to work.我尝试了各种东西,例如 '[^(B)[[:digit:]]{4}[.][[:alnum:]_]+]*' 以及我在这里找到的各种其他东西,但它们似乎都不起作用。 Is this is a quirk of using regex within Oracle?
这是在 Oracle 中使用正则表达式的怪癖吗?
Just to clarify, after running the code I would be wanting something that looked like this:只是为了澄清一下,在运行代码后,我想要看起来像这样的东西:
' B1234.CAT_DOG_MOUSE B3456.BIRD_SHOE '
Any help would be hugely appreciated.任何帮助将不胜感激。
Thanks!谢谢!
You can use您可以使用
regexp_replace(jibberish, '(B\d{4}\.\w+)?.', '\1 ')
See the regex demo .请参阅正则表达式演示。
Details细节
(B\d{4}\.\w+)?
- an optional group matching B
, then four digits, a dot and then one or more word chars B
,然后是四个数字,一个点,然后是一个或多个单词字符.
- any one char. The replacement is \1
, the backreference to the value captured by the capturing group.替换是
\1
,对捕获组捕获的值的反向引用。
If there can be consecutive matches , add one more REGEXP_REPLACE
call to append a space right after your expected matches as a workaround:如果可以有连续的匹配项,请在预期匹配项之后立即向 append 添加一个
REGEXP_REPLACE
调用作为解决方法:
regexp_replace(regexp_replace(jibberish, '(B\d{4}\.\w+)', '\1 '), '(B\d{4}\.\w+)?.', '\1 ')
This might add a couple extract spaces, though, but all consecutive matches will be found.不过,这可能会添加几个提取空间,但会找到所有连续的匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.