<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

    業(yè)精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      397 隨筆 :: 33 文章 :: 29 評(píng)論 :: 0 Trackbacks
    10G中正則表達(dá)式的應(yīng)用
    ?
    ??? 在ITPUB上看到個(gè)帖子,計(jì)算四則運(yùn)算的,順便來(lái)學(xué)習(xí)一下10g里的正則表達(dá)式:
    ?
    ??? 原帖地址: http://www.itpub.net/viewthread.php?tid=1051167&extra=page%3D1%26amp%3Bfilter%3Ddigest
    ?
    ??? 把樓主計(jì)算四則運(yùn)算的SQL貼一下,加了一些我自己的注釋:
    ?

    select a.id,

    ?????? max (text) text,

    ?????? sum (regexp_substr(add_text, '[0-9]+' , 1 ,n) -- 依次找出第 N 個(gè)數(shù)字

    ?????????? *

    ?????????? decode(regexp_substr( '+' ||add_text, '[^0-9]' , 1 ,n), '+' , 1 ,- 1 )) -- 依次找出 +|- ,然后在后面的數(shù)字上乘以系數(shù)

    ?????? -- 以上 sum 計(jì)算了所有 +|- 運(yùn)算的總合計(jì)值

    ?????? +

    ?????? nvl( sum (( select decode(substr(regexp_substr( '+' ||text, '[+|-]([0-9]+[*|/]+)+[0-9]+' , 1 ,n), 1 , 1 ), '+' , 1 ,- 1 )

    ???????????????????????????????????? -- 找出 +|- 開(kāi)頭,并緊跟數(shù)字、 [*|/] 、數(shù)字的部分,依次根據(jù)第一位來(lái)判定系數(shù)

    ?????? *

    ?????? power( 10 , Sum ( Log ( 10 ,decode(regexp_substr( '*' ||regexp_substr(text, '([0-9]+[*|/]+)+[0-9]+' , 1 ,n), '[^0-9]' , 1 , rownum ),

    ???????????????????????????????????????????????????? -- 找出第 n 個(gè)數(shù)字、 [*|/] 、數(shù)字相連的部分

    ????????????????????????????????? -- 排除數(shù)字,找出前面找到的部分中的第 rownum 個(gè)非數(shù)字的字符 ( 最前面加 *)

    ????????????????????????? ??????? '*' ,

    ????????????????????????????????? regexp_substr(regexp_substr(text, '([0-9]+[*|/]+)+[0-9]+' , 1 ,n), '[0-9]+' , 1 , rownum ),

    ????????????????????????????????? -- 如果是 '*' 則,則直接找到 * 后面的數(shù)字部分

    ????????????????????????????????? 1 /regexp_substr(regexp_substr(text, '([0-9]+[*|/]+)+[0-9]+' , 1 ,n), '[0-9]+' , 1 , rownum )

    ????????????????????????????????? -- 如果不是 '*'( /) ,則用 1/NUM

    ????????????????????????????????? ))))

    ?????? -- 外層通關(guān) LOG POWER 函數(shù),把乘除法轉(zhuǎn)換為加減法

    ?????? from dual connect by rownum <=len) -- 在這里再做一層循環(huán),用于計(jì)算乘除法

    ?????? ) , 0 ) wanted

    ? from ?

    ?????? ( select a.id,

    ?????????????? a.text,

    ?????????????? length(regexp_replace(text, '[0-9]+' ))+ 1 len, -- 去掉數(shù)字計(jì)算運(yùn)算符個(gè)數(shù)

    ?????????????? regexp_replace(text, '([0-9]+[*|/]+)+[0-9]+' , 0 ) add_text -- *|/ 操作的數(shù)均用 0 代替

    ????????? from t_mar a) a,

    ?????? ( select rownum n from dual connect by rownum < 100 ) b

    ? where a.len>=b.n -- 可以直接形成從 1 a.len 的循環(huán)操作

    ? group by id

    ? order by id ;

    ?

    ?
    ??? 除了一些轉(zhuǎn)化、分類的思想之外,主要就是用到了正則表達(dá)式,再把Oracle 10g中的正則表達(dá)式規(guī)則也貼一下:
    ?

    \ ???? The backslash character can have four different meanings depending on

    ????? the context. It can:

    ?????? Stand for itself

    ?????? Quote the next character

    ?????? Introduce an operator

    ?????? Do nothing

    * ???? Matches zero or more occurrences

    + ???? Matches one or more occurrences

    ? ???? Matches zero or one occurrence

    | ???? Alternation operator for specifying alternative matches

    ^ ???? Matches the beginning of a string by default. In multiline mode, it matches

    ????? the beginning of any line anywhere within the source string.

    $ ???? Matches the end of a string by default. In multiline mode, it matches the

    ?? ?? end of any line anywhere within the source string.

    . ???? Matches any character in the supported character set except NULL

    [ ] ?? Bracket expression for specifying a matching list that should match any

    ????? one of the expressions represented in the list. A nonmatching list

    ????? expression begins with a circumflex (^) and specifies a list that matches

    ????? any character except for the expressions represented in the list.

    ( ) ?? Grouping expression, treated as a single subexpression

    {m} ?? Matches exactly m times

    {m,} ? Matches at least m times

    {m,n} Matches at least m times but no more than n times

    \n ??? The backreference expression (n is a digit between 1 and 9) matches the nth

    ????? subexpression enclosed between '(' and ')' preceding the \n

    [..] ? Specifies one collation element, and can be a multicharacter element (for

    ????? example, [.ch.] in Spanish)

    [: :] Specifies character classes (for example, [:alpha:]). It matches any character

    ????? within the character class.

    [==] ? Specifies equivalence classes. For example, [=a=] matches all characters

    ????? having base letter 'a'.

    ?
    ?
    ??? 這樣就比較完整了,至于regexp_substrregexp_replace主要查詢《SQL Reference》就可以了
    ?
    ?
    ?
    ?
    ---------------------------------------------------------------------------
    ?
    學(xué)習(xí)一下:
    ?

    select text,regexp_replace(text, '[0-9]' , 0 ) from t_mar -- 所有數(shù)字每個(gè)都用 0 代替

    select text,regexp_replace(text, '[0-9]+' , 0 ) from t_mar -- 所有數(shù)字相鄰的用 1 個(gè) 0 代替

    select text,regexp_replace(text, '[*|/]' , 0 ) from t_mar -- 所有 * / 每個(gè)都用 0 代替

    select text,regexp_replace(text, '[*|/]+' , 0 ) from t_mar -- 所有 * / 相鄰的用 1 個(gè) 0 代替

    select text,regexp_replace(text, '[0-9]+[*|/]+' , 0 ) from t_mar -- 數(shù)字和 * 或數(shù)字和 / 相鄰的用 1 個(gè) 0 代替

    select text,regexp_replace(text, '([0-9]+[*|/]+)+[0-9]+' , 0 ) from t_mar -- 數(shù)字和 *|/ 和數(shù)字相鄰的用 1 個(gè) 0 代替

    select text,regexp_replace(text, '[^0-9]' , 0 ) from t_mar -- 除了數(shù)字別的字符每個(gè)都用 0 代替

    select text,regexp_substr(text, '([0-9]+[*|/]+)+[0-9]+' , 1 , 1 ) from t_mar -- 找到第 1 個(gè)數(shù)字、 [*|/] 、數(shù)字相連的部分

    select regexp_substr( '34*66_50#$@(97)5' , '[^0-9]' , 2 , 4 ) from dual -- 找到從第 2 位開(kāi)始的第 4 個(gè)非數(shù)字字符

    ?

    ?

    posted on 2008-12-29 21:17 decode360 閱讀(193) 評(píng)論(0)  編輯  收藏 所屬分類: 05.SQL
    主站蜘蛛池模板: 亚洲日韩在线视频| 国产精品久久久久久久久久免费 | 一个人免费观看视频www| 亚洲精品成人在线| 亚洲一区二区三区在线观看蜜桃 | 亚洲视频在线观看免费视频| a毛片基地免费全部视频| 亚洲一区二区三区偷拍女厕| 亚洲成AV人片在线观看无码| 最新亚洲人成无码网www电影| 四虎在线最新永久免费| 亚洲人成网站在线观看播放| 婷婷亚洲综合一区二区| 久久久久国色AV免费观看性色 | 中文字幕日韩亚洲| 亚洲成在人线aⅴ免费毛片| 久久九九兔免费精品6| 亚洲精品无码专区久久久| 美女羞羞喷液视频免费| 亚洲欧洲日产国码www| a级毛片视频免费观看| 69xx免费观看视频| 亚洲av一综合av一区| 日韩精品无码一区二区三区免费 | 亚洲精品GV天堂无码男同| 久视频精品免费观看99| 国产亚洲成av片在线观看| 在线观看免费黄网站| 亚洲美女在线国产| 老司机午夜在线视频免费观| 处破痛哭A√18成年片免费| 亚洲成人黄色在线观看| 一级毛片免费视频| 久久久无码精品亚洲日韩蜜桃 | 亚洲色欲色欲www| 免费人成在线观看69式小视频| 婷婷久久久亚洲欧洲日产国码AV| 中文精品人人永久免费| 亚洲中文字幕无码久久综合网| 一级毛片在线免费播放| 亚洲第一页综合图片自拍|