[英]Using REGULAR EXPRESSION to replace string between special characters in oracle
从对偶中选择a [b] c [d] [e] f [g];
我需要一个输出: acf
即与所有[]以及它们之间的文本一起删除。
解决方案可以在Oracle或C ++函数中。
在C ++中尝试了擦除功能,类似于:
int main ()
{
std::string str ("a[b]c[d]e[f]");
std::cout << str << '\n';
while(1)
{
std::size_t foundStart = str.find("[");
//if (foundStart != std::string::npos)
std::cout << "'[' found at: " << foundStart << '\n';
str.begin();
std::size_t foundClose = str.find("]");
//if (foundClose != std::string::npos)
std::cout << "']' found at: " << foundClose << '\n';
str.begin();
str.erase (foundStart,foundClose);
std::cout << str << '\n';
}
return 0;
}
返回的输出为:
a[b]c[d]e[f]
'[' found at: 1
']' found at: 3
ac[d]e[f]
'[' found at: 2
']' found at: 4
ac[f]
'[' found at: 2
']' found at: 4
ac
'[' found at: 18446744073709551615
']' found at: 18446744073709551615
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
提前致谢。
我不知道足够的C ++或Oracle来实现它,但是我想正则表达式看起来像这样:
(?<=[\\s\\]])[az](?=(\\[[az]\\])+[\\sa-z])
这将匹配a , c和f 。
您将需要遍历匹配项并相应地进行打印。
正则表达式与目标周围的任何文本分离,
你好,有[b] c [d] [e] f [g]种方式! 将有相同的比赛,
只要确保目标字符串a [b] c [d] [e] f [g]周围有空格
希望对您有帮助!
祝好运
您可以使用regexp_replace(<your_string>,'\\[.*?\\]')
崩溃了
\[ --matches single square bracket '['. Should be escaped with a backslash '\', as '[' is regex operator
.*? --non greedy expression to match minimum text possible
\] --matches single square bracket ']'. Should be escaped with a backslash '\', as ']' is regex operator
例:
SQL> with x(y) as (
select 'a[b]c[d][][e]f[g][he]ty'
from dual
)
select y, regexp_replace(y,'\[.*?\]') regex_str
from x;
Y REGEX_STR
----------------------- -----------
a[b]c[d][][e]f[g][he]ty acfty
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.