posts - 56, comments - 77, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          class IP

              include RegexpBuilder

              def less_than_255

                  _0_to_100 = repeat(digit(), 1, 2)

                  _100_to_200 = "1" + _0_to_100

                  _200_to_250 = "2" + either("0-4") + either("0-9")

                  _250_to_255 = "25" + either("0-4")

                 one_of(_0_to_100, _100_to_200, _200_to_250, _250_to_255)

              end

              def initialize

                  @ip_pattern = repeat(group(group(less_than_255()) + literal(".")), 3) + group(less_than_255())

              end

              attr_reader :ip_pattern

          end


          以上是應(yīng)用 RegexBuilder 書寫正則表達式例如 IP 地址的一個例子.

          RegexBuilder 的目的是試圖增強正則表達式的可讀性和可維護性

          RegexBuilder 并不是用來取代 Regexp 的, 而是輔助編寫 Regexp 構(gòu)造函數(shù)需要的第一個參數(shù).

          RegexBuilder 包含了正則表達式符號(Anchors/Character Classes/Repetition/Alternation/Grouping)到 API 的一一對應(yīng), 如 one_of => |, either => [], at_least_one => + 等.

          RegexBuilder 還包含了可以直接使用的一組常見的正則表達式, 如 IP 地址, Email 地址等.

           

          RegexBuilder 使用了Java風(fēng)格的API而不是Ruby風(fēng)格的API, 如 repeat(3, digit()) 而不是 digit(:repeat => 3)

          RegexBuilder 還沒實現(xiàn)Substitutions, 如 \0, \1, \& 等.

           

          安裝下載

          gem install regexbuilder

          http://roll-stone.googlecode.com/svn/trunk/RegexBuilder/


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 内丘县| 南漳县| 安吉县| 高淳县| 桑植县| 乌兰察布市| 涞源县| 清远市| 盈江县| 榆林市| 台中市| 广丰县| 漳州市| 高清| 西畴县| 永昌县| 泸西县| 昭平县| 双鸭山市| 井陉县| 商丘市| 闻喜县| 廉江市| 厦门市| 台江县| 镇远县| 白城市| 丰顺县| 米脂县| 灌南县| 类乌齐县| 柳河县| 增城市| 大厂| 昌宁县| 牡丹江市| 卢湾区| 巴彦淖尔市| 体育| 略阳县| 什邡市|