Ruby正规表达式
#构造正则表达式的3种方式puts a=Regexp.new('^s*')
b=/^s*/
c=%r{^\s*}
name="Fats Waller"
puts name=~/a/#返回 a 的字符位置
name=~/z/
puts /a/=~name
def show_regexp(a,re)
if a=~re
"#{$`}<<#{$&}>>#{$'}" #$`得到匹配之前的那部分字符串,$&得到匹配那部分字符串,$'得到匹配之后那部分字符串
else
"no match"
end
end
#特殊字符 . | ( ) [ ] { } + \ ^ $ *?
#之前放置 \ 匹配它们的字面量
puts show_regexp('very interesting',/t/)
puts show_regexp('yes|no',/\|/) #\|匹配 |
puts show_regexp('yes (no)',/\(no\)/) #\( , \)
#位置匹配 ^ 行首 $行尾 \A字符串开始 \z和\Z 匹配字符串的结尾
puts show_regexp("this is\nthe time",/^the/)
puts show_regexp("this is\nthe time",/is$/)
puts show_regexp("this is\nthe time",/\Athis/)
puts show_regexp("this is\nthe time",/\Athe/)
puts "sso good".gsub!(/\z/,"p")
puts "sso".gsub!(/.$/,"p")
puts "sos".gsub!(/.\z/,"a")
#\b 匹配词的边界 \B匹配非词的边界
puts show_regexp("this is\nthe time",/\bis/)
puts show_regexp("this is\nthe time",/\Bis/)
#字符类 处于方括号之间的集合
#[:digit:]是POSIX字符类
puts show_regexp("Price $12.",//)
puts show_regexp("Price $12.",/[\s]/)
puts show_regexp("Price $12.",/[[:digit:]]/)
puts show_regexp("Price $12.",/[[:space:]]/)
puts show_regexp("Price $12.",/[[:punct:]aeiou]/)
a='see '
puts show_regexp(a,//)
puts show_regexp(a,//)
puts show_regexp(a,/[\]]/)
puts show_regexp(a,/[-]/)
puts show_regexp(a,/[^a-z]/)
puts show_regexp(a,/[^a-z\s]/)
puts show_regexp(a,/\d/)
#.匹配除回车换行符之外的任何字符(在多行模式下它也匹配回车)
a='It costs $12.'
puts show_regexp(a,/c.s/)
puts show_regexp(a,/./)
puts show_regexp(a,/\./)
#重复
#r代表模式内先前的正则表达式
#r* 匹配零个或多个r的出现
#r+ 匹配1个或多个r的出现
#r? 匹配0个或1个r的出现
#r{m,n} 匹配至少"m"次r和最多"n"次的出现
#r{m,} 匹配至少"m"次r的出现
#r{m} 只匹配"m"次r的出现
a="the moon is made of cheese"
puts show_regexp(a,/\w+/)
puts show_regexp(a,/\s.*\s/)
puts show_regexp(a,/\s.*?\s/)
puts show_regexp(a,/{2,99}/)
puts show_regexp(a,/mo?o/)
#替换
#|匹配它之前或之后的正则表达式
a="red ball blue sky"
puts show_regexp(a,/d|e/)
puts show_regexp(a,/al|lu/)
puts show_regexp(a,/red ball|angry sky/)
puts show_regexp('banana',/an*/)
puts show_regexp('banana',/(an)*/)
puts show_regexp('banana',/(an)+/)
puts show_regexp(a,/blue|red/)
puts show_regexp(a,/(blue|red) \w+/)
puts show_regexp(a,/red|blue \w+/)
puts show_regexp(a,/red (ball|angry) sky/)
a='the red angry sky'
puts show_regexp(a,/red (ball|angry) sky/)
#括号也收集模式匹配的结果
"12:50am"=~/(\d\d):(\d\d)(..)/
puts "Hour is #$1,minute #$2"
"12:50am"=~/((\d\d):(\d\d))(..)/
puts "Time is #$1"
puts "Hour is #$2,minute #$3"
puts "AM/PM is #$4"
#\1 表示第一个组的匹配 \2表示第二个组的匹配
puts show_regexp('He said "Hello"',/(\w)\1/)
puts show_regexp('Mississipi',/(\w+)\1/)
#也可以向后引用去匹配分界符号
puts show_regexp('He said "Hello"',/(["']).*?\1/)
puts show_regexp("He said 'Hello'",/(["']).*?\1/)
#基于模式的替换
a="the quick brown fox"
puts a.sub(//,'*')
puts a.gsub(//,'*')
puts a.sub(/\s\S+/,'')
puts a.gsub(/\s\S+/,'')
a="the quick brown fox"
puts a.sub(/^./){|match|match.upcase}
puts a.gsub(//){|vowel|vowel.upcase}
def mixed_case(name)
name.gsub(/\b\w/){|first|first.upcase}
end
#替换中的反斜线序列
puts mixed_case("fats waller")
puts "fred:smith".sub(/(\w+):(\w+)/,'\2,\1')
puts "nercpyitno".gsub(/(.)(.)/,'\2\1')
#\&(最后的匹配),\+(最后匹配的组),\'(匹配之前的字符串),\\(字面量反斜线)
str='a\b\c'
puts str.gsub(/\\/,'\\\\\\\\')
puts str.gsub(/\\/,'\&\&')
puts str.gsub(/\\/){'\\\\'}
#面向对象的正则表达式
re=/(\d+):(\d+)/
md=re.match("Time: 12:34am")
puts md # ==$&
puts md #==$1
puts md # ==$2
puts md.pre_match # == $`
puts md.post_match # ==$'
#$~保存线程局部变量
re=/(\d+):(\d+)/
md1=re.match("Time: 12:34am")
md2=re.match("Time: 10:30pm")
puts [$1,$2]
$~=md1
puts [$1,$2]
页:
[1]