使用REGULAR EXPRESSION替换oracle中特殊字符之间的字符串

时间:2014-05-15 11:47:36

标签: c++ oracle11g

从双重选择[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

先谢谢。

2 个答案:

答案 0 :(得分:0)

我不太了解C ++或Oracle来实现它,但我认为正则表达式看起来像这样:

(?<=[\s\]])[a-z](?=(\[[a-z]\])+[\sa-z])

这将匹配 a c f

您需要迭代匹配并相应地打印它们。

正则表达式与目标周围的任何文本分离,
你好,[b] c [d] [e] f [g]的方式去!将有相同的匹配,
只要确保在目标字符串周围留出空格a [b] c [d] [e] f [g]

我希望我能帮到你!

祝你好运

答案 1 :(得分:0)

您可以使用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