如何使用regexp_substr拆分oracle中的单词?

时间:2016-11-07 02:42:41

标签: sql oracle

我有一个包含3个部分的字符串,整个字符串是:

`aUNITED STATES :bUSCOMPANY,c7/1990.`            

如何在oracle中使用regexp_substr分别获得不带小写字符的3个部分?

结果应为:

UNITED STATES :
USCOMPANY,
7/1990.

我尝试过以下代码。

select REGEXP_SUBSTR('aUNITED STATES :bUSCOMPANY,c7/1990.', '[^a-z]+') 
from dual;

我可以成功获得UNITED STATES :,但我怎样才能获得其他结果?

3 个答案:

答案 0 :(得分:1)

您可以使用substrinstr的组合来完成此操作,因为字符串模式是一致的。

select 
--replace col with the column name from the table
substr(col,2,instr(col,':')-1) part_1,
substr(col,instr(col,':')+2,instr(col,',')-instr(col,':')-1) part_2,
substr(col,instr(col,',')+1) part_3
from yourtable

答案 1 :(得分:1)

select  regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','^a([^b]*)',1,1,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','b([^c]*)' ,1,1,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','c(.*)$'   ,1,1,'c',1)

from    dual
;

select  regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[a-c]([^a-c]*)',1,1,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[a-c]([^a-c]*)',1,2,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[a-c]([^a-c]*)',1,3,'c',1)

from    dual
;

select  regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[^a-c]*',1,2)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[^a-c]*',1,4)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','[^a-c]*',1,6)

from    dual
;

select  regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','^a(.*?)b',1,1,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','b(.*?)c' ,1,1,'c',1)
       ,regexp_substr ('aUNITED STATES :bUSCOMPANY,c7/1990.','c(.*)$'  ,1,1,'c',1)

from    dual
;

答案 2 :(得分:0)

1)Fisrt选项。用regexp。

 select xmlcast(c.column_value as varchar2(200)) from xmltable('for $i in ora:tokenize($str,"[a-z]")
                                               where string-length($i) >0
                                                return $i' passing 'aUNITED STATES :bUSCOMPANY,c7/1990.' as "str" ) c;

regexp_substr允许从字符串regexp_substr - desc中提取regexp组。该小组由定义 括号"(.......)"。

2)xquery / xmltable

ora:tokenize($str,"[a-z]")

AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); AdListener listener = new AdListener() { @Override public void onAdClosed() { super.onAdClosed(); Log.i("AdListener", "onAdClosed"); } @Override public void onAdFailedToLoad(int errorCode) { super.onAdFailedToLoad(errorCode); Log.i("AdListener", "onAdFailedToLoad---->"+errorCode); } @Override public void onAdLeftApplication() { super.onAdLeftApplication(); Log.i("AdListener", "onAdLeftApplication"); } @Override public void onAdLoaded() { super.onAdLoaded(); Log.i("AdListener", "onAdLoaded"); } @Override public void onAdOpened() { super.onAdOpened(); Log.i("AdListener", "onAdOpened"); } }; mAdView.setAdListener(listener); mAdView.loadAd(adRequest); - 使用reg_exp模式将字符串拆分为字符串sequnect。 xmltable中的语法被称为" FLWOR表达式"