在sql查询结果

时间:2016-09-16 09:47:20

标签: sql regex oracle

我正在使用oracle 11g,我有一个sql查询返回一个字符串类型result_column,其中包含以下文本格式(它是通过listagg函数获得的串联):

aaa,bbbbbbb,cc,dddddddddd

我不知道"操作数"的数量,它可以是2,3,4甚至更多......

我希望HTML格式化结果以便替换颜色,所以我想将结果列转换为:

<font color="red">aaa,</font> <font color="green">bbbbbbb,</font> <font      color="red">cc,</font> <font color="green">dddddddddd</font>
这可能吗? (通过正则表达式?)

提前感谢您提供的任何提示

亲切的问候 果酱

2 个答案:

答案 0 :(得分:0)

这是一种方式,虽然它非常笨重(它需要regexp_replace和普通替换):

WITH sample_data AS (SELECT 'aaa, bbbbbbb, cc, dddddddddd' str FROM dual UNION ALL
                     SELECT 'aaa' str FROM dual UNION ALL
                     SELECT 'aaaa, bbbbbbb' str FROM dual UNION ALL
                     SELECT 'aa, bbbbbb, ccc' str FROM dual UNION ALL
                     SELECT 'aaaa, b, ccc, dddd' str FROM dual)
SELECT str,
       replace(regexp_replace(str, 
                              '( ?)([^,]+)(,|$) ?([^,]*)(,|$)',
                              '\1<font color="red">\2\3</font> <font color="green">\4\5</font>'),
               '<font color="green"></font>') new_str
FROM   sample_data;

STR                          NEW_STR
---------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aaa, bbbbbbb, cc, dddddddddd <font color="red">aaa,</font> <font color="green">bbbbbbb,</font> <font color="red">cc,</font> <font color="green">dddddddddd</font>
aaa                          <font color="red">aaa</font>
aaaa, bbbbbbb                <font color="red">aaaa,</font> <font color="green">bbbbbbb</font>
aa, bbbbbb, ccc              <font color="red">aa,</font> <font color="green">bbbbbb,</font> <font color="red">ccc</font>
aaaa, b, ccc, dddd           <font color="red">aaaa,</font> <font color="green">b,</font> <font color="red">ccc,</font> <font color="green">dddd</font>

基本上,这会搜索我们有0或1个空格后跟任意数量的非逗号字符的模式,后跟逗号或行尾。然后可以跟随一个类似的模式,或者不是(因此在正则表达式模式的后半部分中的*与前半部分中的+相比),因为我们可能在字符串中有奇数个元素。

然后它使用搜索模式中括号中的元素进行替换(每个括号中的元素可以用\ N引用,其中N是它在模式字符串中的出现)。

最后,考虑到奇数元素将留下尾随<font color="green"></font>的事实,我们需要将其剥离 - 因此外部replace

我愿意打赌,有更多有效的解决方案,但是!

答案 1 :(得分:0)

理论上,如果值的数量是有限的,可以很简单地完成,例如最多10个。它不漂亮(事实上,它非常丑陋;)但是它有效。取代

([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?

<font color="red">\1</font> <font color="green">\2</font> <font color="red">\3</font> <font color="green">\4</font> <font color="red">\5</font> <font color="green">\6</font> <font color="red">\7</font> <font color="green">\8</font> <font color="red">\9</font> <font color="green">\10</font>

See it here at regex101

一个javascript片段来说明。

var regex = /([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?([^,]*,?)?/,
    repl = '<font color="red">$1</font> <font color="green">$2</font> <font color="red">$3</font> <font color="green">$4</font> <font color="red">$5</font> <font color="green">$6</font> <font color="red">$7</font> <font color="green">$8</font> <font color="red">$9</font> <font color="green">$10</font>',
    input = 'aaa, bbbbbbb, cc, dddddddddd';

document.getElementById('output').innerHTML = input.replace(regex, repl);
<div id="output"/>