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

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

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

    Decode360's Blog

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

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

    select a.id,

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

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

    ?????????? *

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

    ?????? -- 以上 sum 計算了所有 +|- 運算的總合計值

    ?????? +

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

    ???????????????????????????????????? -- 找出 +|- 開頭,并緊跟數字、 [*|/] 、數字的部分,依次根據第一位來判定系數

    ?????? *

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

    ???????????????????????????????????????????????????? -- 找出第 n 個數字、 [*|/] 、數字相連的部分

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

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

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

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

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

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

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

    ?????? -- 外層通關 LOG POWER 函數,把乘除法轉換為加減法

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

    ?????? ) , 0 ) wanted

    ? from ?

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

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

    ?????????????? length(regexp_replace(text, '[0-9]+' ))+ 1 len, -- 去掉數字計算運算符個數

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

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

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

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

    ? group by id

    ? order by id ;

    ?

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

    \ ???? 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》就可以了
    ?
    ?
    ?
    ?
    ---------------------------------------------------------------------------
    ?
    學習一下:
    ?

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

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

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

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

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

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

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

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

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

    ?

    ?

    posted on 2008-12-29 21:17 decode360 閱讀(205) 評論(0)  編輯  收藏 所屬分類: 05.SQL
    主站蜘蛛池模板: 三级片免费观看久久| 精品国产免费一区二区三区香蕉| 国产一级特黄高清免费大片| 特级无码毛片免费视频| 亚洲日韩精品一区二区三区无码 | 久热中文字幕在线精品免费| 亚洲性无码一区二区三区| 国产亚洲人成A在线V网站| 又粗又大又黑又长的免费视频| 国产精品亚洲а∨天堂2021 | 亚洲精品一卡2卡3卡四卡乱码| 婷婷综合缴情亚洲狠狠尤物| 未满十八18禁止免费无码网站| 亚洲影视自拍揄拍愉拍| AV在线播放日韩亚洲欧| 我的小后妈韩剧在线看免费高清版 | 国产日韩AV免费无码一区二区三区| 好看的亚洲黄色经典| 2020久久精品国产免费| 一级做a爱片特黄在线观看免费看 一级做a爱过程免费视 | 成年女人午夜毛片免费视频| 热99RE久久精品这里都是精品免费| 亚洲一卡一卡二新区无人区| 亚洲国产成人片在线观看| 亚洲精品免费在线| 青娱乐免费视频在线观看| 激情小说亚洲图片| 亚洲v高清理论电影| 国产一区二区三区在线免费 | 亚洲成亚洲乱码一二三四区软件| 日韩在线免费播放| 久久午夜羞羞影院免费观看| 国产乱妇高清无乱码免费| 亚洲中文字幕无码中文字| 亚洲视频中文字幕| 亚洲色偷拍另类无码专区| 国产免费资源高清小视频在线观看| 中文字幕视频免费| 国产精品福利片免费看| 久久精品国产亚洲av天美18| 亚洲天堂2017无码中文|