【问题标题】:Using REGULAR EXPRESSION to replace string between special characters in oracle使用正则表达式替换oracle中特殊字符之间的字符串
【发布时间】:2014-05-16 12:13:59
【问题描述】:

从对偶中选择 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++ oracle11g


    【解决方案1】:

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

    (?&lt;=[\s\]])[a-z](?=(\[[a-z]\])+[\sa-z])

    这将匹配 a、c 和 f。

    您将需要遍历匹配项并相应地打印它们。

    正则表达式与目标周围的任何文本解耦,
    你好,有 [b]c[d][e]f[g] 方法! 将有相同的比赛,
    只需确保在目标字符串 a[b]c[d][e]f[g]

    周围有空格

    希望对你有所帮助!

    祝你好运

    【讨论】:

      【解决方案2】:

      您可以使用regexp_replace(&lt;your_string&gt;,'\[.*?\]')

      崩溃,

      \[      --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
      

      【讨论】:

        猜你喜欢
        • 2014-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        相关资源
        最近更新 更多