我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Groovy系列之旅四(正則表達式基礎)

          正則表達式是字符串處理利器.簡單的正則就是一些字符或數字.
          包含一個最簡單正則表達式的表達式是用==~.
          比如說:
          "beijing" ==~ /beijing/
          運行一下結果是true.
          在這里有兩件需要注意的事情,第一是
          ==~它和==操作符很像,但正則匹配模式已經代替了精確比較.
          第二個是/,把表達式包含在/ 里面就是告訴groovy,把它當成正則
          處理,而不是字符串.

          我們可以在beijing后面加上?,表達字符g是可選的.

          "beijing" ==~ /beijing/
          "beijin" ==~/beijing?/
          結果都為true.

          現在我們定義一個方法,來檢驗指定字符串是否符合我們給定的
          正則:
          def check(a,b)
          {
              
          if(a ==~ b)
              
          {
                  println(
          "spell ok")
              }

              
          else
              
          {
                  println(
          "error,try again")
              }

          }
          鑒于剛才上面提到的基礎知識,我們輸入如下內容進行測試:
          regular = /zdw/
          check(
          "zdw" , regular)
          check(
          "test", regular)
          theRegularExpression 
          = /Wisniew?ski/
          check(
          "Wisniewski", theRegularExpression)
          check(
          "Wisnieski", theRegularExpression)
          check(
          "Wisniewewski", theRegularExpression)

          輸出結果是:
          spell ok
          error,
          try again
          spell ok
          spell ok
          error,
          try again

          假定有字符串"lovebeijing" ,如果我們想讓"beijing"變成可選的,比如換成"nanjing",這時
          可以用(beijing | nanjing),注意|兩邊不能有空格:
          regular = /love(beijing|nanjing)/    
          check(
          "lovebeijing",regular)
          check(
          "lovenanjing",regular)

          上面兩個表達式都是符合要求的.

          (a | b) 只能出現其中和一個,且一次.

          我們再來看看其它一些例子:
          theRegularExpression = /Wis[abcd]niewski/ // 'a', 'b', 'c', 'd' 其中的一個
          theRegularExpression = /Wis[abcd]?niewski/ // 'a', 'b', 'c', 'd' 其中的一個,但不是必須的
          theRegularExpression = /Wis[a-zA-Z]niewski/ // 必須有一個從a-z的大寫或小寫字符出現
          theRegularExpression = /Wis[^abcd]niewski/ // 除'a','b','c','d'以外其它字符

          正則表達式符號的說明:

          Regular Expression Operators
          a?  matches 
          0 or 1 occurrence of *a*  'a' or empty string  
          a*  matches 
          0 or more occurrences of *a*  empty string or 'a', 'aa', 'aaa', etc  
          a+  matches 
          1 or more occurrences of *a*  'a', 'aa', 'aaa', etc  
          a|b  match *a* or *b*  'a' or 'b' -  
          .  match any single character  'a'
          , 'q', 'l', '_', '+', etc  
          [woeirjsd]  match any of the named characters  'w', 'o', 'e', 'i', 'r', 'j', 's', 'd'  
          [1-9]  match any of the characters in the range  '1', '2', '3', '4', '5', '6', '7', '8', '9'  
          [^13579]  match any characters not named  even digits, or any other character  
          (ie)  group an expression (for use with other operators)  'ie'  
          ^a  match an *a* at the beginning of a line  'a'  
          a$  match an *a* at the end of a line  'a'  

          有件事情你是必須知道的,假如你想匹配一個上面有特殊意思的符號,比如?,
          你必須在?前面加'\'進行轉義.

          我們來看看這個例子:
          check("I love beijing?",/[^\?]+\?/)
          這可能是你遇到的第一個難看的正則.然而這種情況在Perl語言里面是
          經常出現的.
          遇到這種情況,我們可以把規則拆開開看:
          /         正則開始
          [^\?]   不能是?號
          +         出現一次或多次
          \?         匹配?
          /          正則結束

          這樣的話,這個正則就很清楚了.
          輸出為: true.

          posted on 2008-05-12 11:24 々上善若水々 閱讀(1536) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 定州市| 昌平区| 二手房| 民和| 大悟县| 获嘉县| 舞钢市| 武安市| 兰溪市| 修文县| 忻州市| 砀山县| 博白县| 宣城市| 工布江达县| 西林县| 海伦市| 安达市| 河东区| 武宁县| 海晏县| 太原市| 榆树市| 象州县| 波密县| 温宿县| 锡林浩特市| 洛隆县| 馆陶县| 柳林县| 孙吴县| 商丘市| 湘潭市| 岳阳市| 株洲县| 株洲市| 大田县| 通山县| 庐江县| 花莲市| 尉犁县|