浏览 18228 次
锁定老帖子 主题:Ruby正则表达式学习笔记一
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-03-17
表达式的一般规则: /a/匹配字符a。 /\?/匹配特殊字符?。特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), +, *. .匹配任意字符,例如/a./匹配ab和ac。 /[ab]c/匹配ac和bc,[]之间代表范围。例如:/[a-z]/ , /[a-zA-Z0-9]/。 /[^a-zA-Z0-9]/匹配不在该范围内的字符串。 /[\d]/代表任意数字,/[\w]/代表任意字母,数字或者_,/[\s]/代表空白字符,包括空格,TAB和换行。 /[\D]/,/[\W]/,/[\S]/均为上述的否定情况。 高级规则: ?代表0或1个字符。/Mrs?\.?/匹配"Mr","Mrs","Mr.","Mrs."。 *代表0或多个字符。/Hello*/匹配"Hello","HelloJavaeye"。 +代表1或多个字符。/a+c/匹配:"abc","abbdrec"等等。 /d{3}/匹配3个数字。 /d{1,10}/匹配1-10个数字。 /d{3,}/匹配3个数字以上。 /([A-Z]\d){5}/匹配首位是大写字母,后面4个是数字的字符串。 String和RegExp均支持=~和match2个查询匹配方法。在irb中: >> "The alphabet starts with abc" =~ /abc/ => 25 >> /abc/.match("The alphabet starts with abc.") => #<MatchData:0x1b0d88> 可以看出,如果能够匹配,=~返回匹配的字符串位置,而match返回一个MatchData对象。如果不匹配返回nil。 MatchData可以取出其中符合各个子匹配的内容。看下面的例子: We have a String : Peel,Emma,Mrs.,talented amateur The order of the name : last name,first name, title, occupation 正则表达式为: /[A-Za-z]+,[A-Za-z]+,Mrs?\./ irb中: >> /[A-Za-z]+,[A-Za-z]+,Mrs?\./.match("Peel,Emma,Mrs.,talented amateur") => #<MatchData:0x401f0a6c> 但是我们想从匹配正则表达式的String里面只取出last name和title相关的字符串,那么正则表达式可以如下: /([A-Za-z]+),[A-Za-z]+,(Mrs?\.)/ 注意([A-Za-z]+) 和 (Mrs?\.),执行下面的代码: >> /([A-Za-z]+),[A-Za-z]+,(Mrs?\.)/.match("Peel,Emma,Mrs.,talented amateur") => #<MatchData:0x401e0a7c> >> puts "Dear #{$2} #{$1}," => Dear Mrs. Peel ()中的表达式就是子表达式。 下面的代码和上面效果一样, m = /([A-Za-z]+),[A-Za-z]+,(Mrs?\.)/.match("Peel,Emma,Mrs.,talented amateur") puts "Dear #{m[2]} #{m[1]}," 这里m[0]返回匹配主表达式的字符串。 下面的方法是等同的: m[n] == m.captures[n] 一些相关的方法,看下面的代码例子: string = "My phone number is (123) 555-1234." phone_re = /\((\d{3})\)\s+(\d{3})-(\d{4})/ m = phone_re.match(string) print "The part of the string before the part that matched was:" puts m.pre_match print "The part of the string after the part that matched was:" puts m.post_match print "The second capture began at character " puts m.begin(2) print "The third capture ended at character " puts m.end(3) Output: The string up to the part that matched was: My phone number is The string after the part that matched was: . The second capture began at character 25 The third capture ended at character 33 下章会写一些高级应用。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |