正则表达式学习笔记

ruby 代码
  1. #构造正则表达式的3种方式   
  2. puts a=Regexp.new('^s*[a-z]')   
  3. b=/^s*[a-z]/   
  4. c=%r{^\s*[a-z]}   
  5.   
  6. name="Fats Waller"  
  7. puts name=~/a/  #返回 a 的字符位置   
  8. name=~/z/   
  9. puts /a/=~name   
  10.   
  11. def show_regexp(a,re)   
  12.   if a=~re   
  13.     "#{$`}<<#{$&}>>#{$'}" #$`得到匹配之前的那部分字符串,$&得到匹配那部分字符串,$'得到匹配之后那部分字符串   
  14.   else  
  15.     "no match"  
  16.   end  
  17. end  
  18.   
  19. #特殊字符 . | ( ) [ ] { } + \ ^ $ *  ?    
  20. #之前放置 \ 匹配它们的字面量   
  21.   
  22. puts show_regexp('very interesting',/t/)   
  23. puts show_regexp('yes|no',/\|/) #\|匹配 |   
  24. puts show_regexp('yes (no)',/\(no\)/) #\( , \)   
  25.   
  26. #位置匹配 ^ 行首 $行尾 \A字符串开始 \z和\Z 匹配字符串的结尾   
  27. puts show_regexp("this is\nthe time",/^the/)   
  28. puts show_regexp("this is\nthe time",/is$/)   
  29. puts show_regexp("this is\nthe time",/\Athis/)   
  30. puts show_regexp("this is\nthe time",/\Athe/)   
  31. puts "sso good".gsub!(/\z/,"p")   
  32. puts "sso".gsub!(/.$/,"p")   
  33. puts "sos".gsub!(/.\z/,"a")   
  34.   
  35. #\b 匹配词的边界 \B匹配非词的边界   
  36. puts show_regexp("this is\nthe time",/\bis/)   
  37. puts show_regexp("this is\nthe time",/\Bis/)   
  38.   
  39.   
  40. #字符类 处于方括号之间的集合   
  41. #[:digit:]是POSIX字符类   
  42. puts show_regexp("Price $12.",/[aeiou]/)   
  43. puts show_regexp("Price $12.",/[\s]/)   
  44. puts show_regexp("Price $12.",/[[:digit:]]/)   
  45. puts show_regexp("Price $12.",/[[:space:]]/)   
  46. puts show_regexp("Price $12.",/[[:punct:]aeiou]/)   
  47.   
  48. a='see [Design Patterns-page 123]'   
  49. puts show_regexp(a,/[A-Fa-f]/)   
  50. puts show_regexp(a,/[0-9][0-9]/)   
  51. puts show_regexp(a,/[\]]/)   
  52. puts show_regexp(a,/[-]/)   
  53. puts show_regexp(a,/[^a-z]/)   
  54. puts show_regexp(a,/[^a-z\s]/)   
  55.   
  56. puts show_regexp(a,/\d/)   
  57.   
  58. #.匹配除回车换行符之外的任何字符(在多行模式下它也匹配回车)   
  59. a='It costs $12.'   
  60. puts show_regexp(a,/c.s/)   
  61. puts show_regexp(a,/./)   
  62. puts show_regexp(a,/\./)   
  63.   
  64. #重复   
  65. #r代表模式内先前的正则表达式   
  66. #r* 匹配零个或多个r的出现   
  67. #r+ 匹配1个或多个r的出现   
  68. #r? 匹配0个或1个r的出现   
  69. #r{m,n} 匹配至少"m"次r和最多"n"次的出现   
  70. #r{m,} 匹配至少"m"次r的出现   
  71. #r{m} 只匹配"m"次r的出现   
  72.   
  73. a="the moon is made of cheese"  
  74. puts show_regexp(a,/\w+/)   
  75. puts show_regexp(a,/\s.*\s/)   
  76. puts show_regexp(a,/\s.*?\s/)   
  77. puts show_regexp(a,/[aeiou]{2,99}/)   
  78. puts show_regexp(a,/mo?o/)   
  79.   
  80. #替换   
  81. #|匹配它之前或之后的正则表达式   
  82. a="red ball blue sky"  
  83. puts show_regexp(a,/d|e/)   
  84. puts show_regexp(a,/al|lu/)   
  85. puts show_regexp(a,/red ball|angry sky/)   
  86.   
  87. puts show_regexp('banana',/an*/)   
  88. puts show_regexp('banana',/(an)*/)   
  89. puts show_regexp('banana',/(an)+/)   
  90. puts show_regexp(a,/blue|red/)   
  91. puts show_regexp(a,/(blue|red) \w+/)   
  92. puts show_regexp(a,/red|blue \w+/)   
  93. puts show_regexp(a,/red (ball|angry) sky/)   
  94.   
  95. a='the red angry sky'   
  96. puts show_regexp(a,/red (ball|angry) sky/)   
  97.   
  98. #括号也收集模式匹配的结果   
  99. "12:50am"=~/(\d\d):(\d\d)(..)/   
  100. puts "Hour is #$1,minute #$2"  
  101. "12:50am"=~/((\d\d):(\d\d))(..)/   
  102. puts "Time is #$1"  
  103. puts "Hour is #$2,minute #$3"  
  104. puts "AM/PM is #$4"  
  105.   
  106. #\1 表示第一个组的匹配 \2表示第二个组的匹配   
  107. puts show_regexp('He said "Hello"',/(\w)\1/)   
  108. puts show_regexp('Mississipi',/(\w+)\1/)   
  109.   
  110. #也可以向后引用去匹配分界符号   
  111. puts show_regexp('He said "Hello"',/(["']).*?\1/)  
  112. puts show_regexp("He said 'Hello'",/(["']).*?\1/)   
  113.   
  114. #基于模式的替换   
  115.   
  116. a="the quick brown fox"  
  117. puts a.sub(/[aeiou]/,'*')   
  118. puts a.gsub(/[aeiou]/,'*')   
  119. puts a.sub(/\s\S+/,'')   
  120. puts a.gsub(/\s\S+/,'')   
  121.   
  122. a="the quick brown fox"  
  123. puts a.sub(/^./){|match|match.upcase}   
  124. puts a.gsub(/[aeiou]/){|vowel|vowel.upcase}   
  125.   
  126. def mixed_case(name)   
  127.   name.gsub(/\b\w/){|first|first.upcase}   
  128. end  
  129.   
  130. #替换中的反斜线序列   
  131. puts mixed_case("fats waller")   
  132. puts "fred:smith".sub(/(\w+):(\w+)/,'\2,\1')   
  133. puts "nercpyitno".gsub(/(.)(.)/,'\2\1')   
  134.   
  135. #\&(最后的匹配),\+(最后匹配的组),\'(匹配之前的字符串),\\(字面量反斜线)   
  136. str='a\b\c'   
  137. puts str.gsub(/\\/,'\\\\\\\\')   
  138. puts str.gsub(/\\/,'\&\&')   
  139. puts str.gsub(/\\/){'\\\\'}   
  140.   
  141.   
  142. #面向对象的正则表达式   
  143. re=/(\d+):(\d+)/   
  144. md=re.match("Time: 12:34am")   
  145. puts md[0] # ==$&   
  146. puts md[1] #==$1   
  147. puts md[2] # ==$2   
  148. puts md.pre_match # == $`   
  149. puts md.post_match # ==$'   
  150.   
  151. #$~保存线程局部变量   
  152. re=/(\d+):(\d+)/   
  153. md1=re.match("Time: 12:34am")   
  154. md2=re.match("Time: 10:30pm")   
  155. puts [$1,$2]   
  156. $~=md1   
  157. puts [$1,$2]  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值