2016-07-28 43 views
1

我loooking的正則表達式表達什麼,從這個:分割由大寫甲骨文

------------------------ 
| id | prop_name | 
------------------------ 
| 1 | isThisAnExample | 
------------------------ 

要這樣:

----------------------------- 
| id | prop_name   | 
----------------------------- 
| 1 | Is This An Example | 
----------------------------- 

當然,如果第一個字符是大寫這將是冷靜和如果其他字以小寫字母開頭。但只有拆分他們也沒關係。

+0

您會在這裏找到你的答案。 http://stackoverflow.com/questions/23470794/split-words-with-a-capital-letter-in-sql –

+2

@Marc這不是Oracle。 – Rene

回答

1

也許這就是你正在尋找

正則表達式「插入每個小寫字母后跟一個大寫字符之間的空白」:

select regexp_replace('IsThisAnExample', '([[:lower:]])([[:upper:]])', '\1 \2') from dual 

第一個字符可以簡單地通過一個大寫更換通過

select upper(substr('isThisAn Example', 1,1))||substr('isThisAn Example', 2) from dual; 

信因此,首先替換的第一個字符和REGEXP_REPLACE的結果:

select regexp_replace(upper(substr('isThisAn Example', 1,1))||substr('isThisAn Example', 2), '([[:lower:]])([[:upper:]])', '\1 \2') from dual; 

如果只有你的句子的第一個字符應該是一個大寫字母,然後嘗試:

select upper(substr(regexp_replace('IsThisAnExample', '([[:lower:]])([[:upper:]])', '\1 \2'),1,1))|| 
     lower(substr(regexp_replace('IsThisAnExample', '([[:lower:]])([[:upper:]])', '\1 \2'),2)) 
from dual 
+0

太好了。現在,如果您可以將解決方案擴展爲:「在每個小寫字符之間插入一個空格,後跟一個大寫字符,並將這些小寫字符放在較高位置,並且該行的第一個字符」將成爲我正在尋找的所有內容 –

+2

完成,看到我的調整:-) –

+0

是的,我們現在有上第一個字符,這是偉大的! 'isThisAnExample' - >'這是一個例子'。現在最後一個請求是'這是一個例子' - >'這是一個例子'。 –

0

更好地使用正則表達式,但無論如何:

SELECT listagg(splitted, '') within GROUP (ORDER BY lvl) FROM( 
    SELECT LEVEL lvl, CASE WHEN SUBSTR(your_string, LEVEL, 1) = 
        UPPER(SUBSTR(your_string, LEVEL, 1)) 
     THEN ' ' || SUBSTR(your_string, LEVEL, 1) ELSE 
        SUBSTR(your_string, LEVEL, 1) END splitted 
FROM (SELECT 'isThisAnExample' your_string FROM dual) 
CONNECT BY LEVEL <= LENGTH(your_string)); 
0

弗蘭克的解決方案類似,但更簡單(儘可能減少正則表達式的使用):

with 
    input (str) as (
     select 'isThisAnExample' from dual 
    ) 
select upper(substr(str, 1, 1)) || 
     lower(regexp_replace(substr(str, 2), '(^|[[:lower:]])([[:upper:]])', '\1 \2')) 
      as modified_str 
from input; 

MODIFIED_STR 
------------------ 
Is this an example 

1 row selected. 
+0

是的,工作正常。做得好 : ) –