我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          Groovy系列之旅四(正則表達(dá)式基礎(chǔ))

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

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

          "beijing" ==~ /beijing/
          "beijin" ==~/beijing?/
          結(jié)果都為true.

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

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

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

          輸出結(jié)果是:
          spell ok
          error,
          try again
          spell ok
          spell ok
          error,
          try again

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

          上面兩個(gè)表達(dá)式都是符合要求的.

          (a | b) 只能出現(xiàn)其中和一個(gè),且一次.

          我們?cè)賮砜纯雌渌恍├?
          theRegularExpression = /Wis[abcd]niewski/ // 'a', 'b', 'c', 'd' 其中的一個(gè)
          theRegularExpression = /Wis[abcd]?niewski/ // 'a', 'b', 'c', 'd' 其中的一個(gè),但不是必須的
          theRegularExpression = /Wis[a-zA-Z]niewski/ // 必須有一個(gè)從a-z的大寫或小寫字符出現(xiàn)
          theRegularExpression = /Wis[^abcd]niewski/ // 除'a','b','c','d'以外其它字符

          正則表達(dá)式符號(hào)的說明:

          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'  

          有件事情你是必須知道的,假如你想匹配一個(gè)上面有特殊意思的符號(hào),比如?,
          你必須在?前面加'\'進(jìn)行轉(zhuǎn)義.

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

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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 民乐县| 泗水县| 连州市| 鄂尔多斯市| 靖西县| 新营市| 新建县| 巴马| 绥阳县| 屏东市| 南充市| 开化县| 武乡县| 建阳市| 诸暨市| 新巴尔虎右旗| 本溪市| 凯里市| 武川县| 贵阳市| 兴和县| 罗田县| 察隅县| 福鼎市| 江永县| 廊坊市| 开鲁县| 辰溪县| 油尖旺区| 苗栗市| 香格里拉县| 巢湖市| 思南县| 仁怀市| 长葛市| 淮北市| 池州市| 喀什市| 巍山| 荥经县| 屯昌县|