Ruby Regexp
創建正則表達式對象
#以大寫字母開始后面緊跟N個數字,方式一 reg = /[A-Z]\d+/ #方式二 reg = Regexp.new("[A-Z]\d+") reg = Regexp.new(/[A-Z]\d+/)?
匹配檢測:=~,當能夠匹配的時候,返回第一次匹配的位置,不能匹配的話返回nil
letter_somenum = /[A-Z|a-z]\d+/if letter_somenum =~ "f1"print "f1 matches regexp" elseprint "f1 not matches regexp" end?
字符串匹配
#只要字符串中含有a到z且緊跟數字的都滿足 /[a-z]\d+/?
匹配行首行為,^和$。
#匹配以大寫字母開頭的表達式 reg = /^[A-Z]/ #匹配末尾是大寫字母的字符串 second = Regexp.new("[A-Z]$") p second =~ "F1" p second =~ "1F" #首位匹配,全部是大寫字母 third = Regexp.new("^[A-Z]+$")?
多個字符選一個使用[]
#ABC三個字符中的任意一個 /[ABC]/ #對于連續的字符可以使用-,匹配任意一個大寫字母 /[A-Z]/ #匹配任意字母或下劃線 /[A-Za-z_]/?
中括號里的^,表示之外的字符
#ABC之外的所有字符 /[^ABC]/?
一個點,匹配任意的單一字符
#以V開頭,以s結尾的所有字符串 never_mind = /^V.*s$/ p never_mind =~ "Voctrals"?
匹配一個空白字符(\s)
#F開頭,N個字母,空格,N個字母結尾 space = /^F[A-Za-z]+\s[A-Za-z]+$/?
匹配一個數字(\d)
#匹配多余0個數字 number = /^\d+$/?
匹配一個英文字母或者一個數字(\w)
#匹配三個字母或者數字 letter_num = /^\w\w\w$/?
前端匹配成功(\A)
#當且僅當前端匹配成功 front = /\Aabc/?
后端匹配成功(\Z),他是應該出現在行尾的。。。
#當且僅當末端匹配成功 back = /abc\Z/?
轉義字符,如\^,\$,\[,\]等,讓他們不再具有特殊意義
?
重復出現
*: 0次以上
#出現0次A,或者N次A /A*/+: 1次以上
#出現N次A,N不允許為0 /A+/?: 0次或一次
#要么出現A,要么就不要出現A,*這里好像有個BUG!!!* /A?/?
最短匹配
#匹配最短的A*B,最好就是AAB /A*?B/ #匹配最短的A?B,最好就是AB /A+?B/?
多個字符重復()
#匹配重復的ABC /(ABC)*/?
或者:|
#匹配ABC或者CBA /(ABC)*|(CBA)*/?
正則表達式的一些選項,直接加載//后面就可以了
i,不區分大小寫
/[a-z]*/is,e,u,n: 指定字符集,默認為n
x: 忽略空白,#之后的內容,這樣就可以給正則表達式加注釋了
/[a-z]+ #N個字母 \d? #一個或沒有數字 /xim:轉換換行符號為.
?
回溯參照:取出滿足條件的字符串部分
在正則表達式中用小括號()括起來的部分,可以使用$1,$2等對應著取出來,ruby會自動的去匹配。
/(.)(..)(.)/ =~ "why????"p $1 #=>"w" p $2 #=>"hy" p $3 #=>"?"如果不想去匹配其中的某些,可以使用?:,放到括號內的前面
/(.)(\d\d)+(.)/ =~ "123456" p $1 #=>1 p $2 #=>45 p $3 #=>6 /(.)(?:\d\d)+(.)/ =~ "123456" p $1 #=>1 p $2 #=>6ruby預留的$`獲取匹配字符前面的部分,$&獲取匹配的字符串,&'獲取匹配字符后面的部分
/Shift/i =~ "control shift alt table" p $` #=>"control " p $& #=>"shift" p $' #=>" alt table"?
使用正則表達式的方法!!!!
sub和gsub,用來替換匹配成功的字符串
sub只會替換第一個匹配成功的字符串,并返回
gsub會替換所有匹配的字符串,并返回
他們都不會修改原字符串,除非加上!,^_^
some= "abc def ghi jk lmn" #把第一個匹配的多個空格替換為_ p some.sub(/\s+/, "_") #=>"abc_def ghi jk lmn" p some #=>"abc def ghi jk lmn" #把所有匹配的多個空格替換為_ p some.gsub(/\s+/, "_") #=>"abc_def_ghi_jk_lmn" p some #=>"abc def ghi jk lmn"?
對滿足的部分進行處理,并返回處理過后的some
some= "abc def ghi jk lmn" # 替換第一個滿足的部分為^_^ matched = some.sub(/\s+/) { |match| "^_^" } p matched #=> "abc^_^def ghi jk lmn"?
scan方法,跟gsub很像,但是它不會進行取代動作,而是對滿足的字符串進行處理:
scantest = "This is a string test for scan" gather = Array.newother = scantest.scan(/i./){|matched|gather.unshift(matched) } p gather #=>["in", "is", "is"]當使用()的時候,如果匹配部分為一個變量,則會傳遞數組給變量,如:
scantest = "This is a string test for scan"gather = Array.newscantest.scan(/(i)(.)/){|matched|gather.unshift(matched) }p gather #=>[["i", "n"], ["i", "s"], ["i", "s"]]如果匹配塊兒中的變量為多個,不會傳遞數組,而是傳遞元素,如:
scantest = "This is a string test for scan"gather = Array.newscantest.scan(/(i)(.)/){|a, b|gather.unshift(a)gather.unshift(b) }p gather #=>["n", "i", "s", "i", "s", "i"]不指定區塊的時候,返回滿足的元素組成的數組
scantest = "This is a string test for scan"gather = Array.newgather = scantest.scan(/i./)p gather #=>["is", "is", "in"]?
轉載于:https://www.cnblogs.com/voctrals/p/4054139.html
總結
以上是生活随笔為你收集整理的Ruby Regexp的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Robot Framework--08
- 下一篇: Exercise 42: Is-A, H