Ruby 简明教程
高级进阶
2. Ruby Regular Expressions 正则表达
正则表达 regular expression 是特殊的字符序列,用模式中句法帮助匹配或查找其它字符串或字符串集。
模式在俩个//之间,或者以%r开头的在任意分隔符之间。 操作符=~。
格式
/pattern/
/pattern/im # option can be specified
%r!/usr/local! # general delimited regular expression
demo_re.rb
line1 = "Cats are smarter than dogs";
line2 = "Dogs also like meat";
if ( line1 =~ /Cats(.*)/ )
puts "Line1 contains Cats"
end
if ( line2 =~ /Cats(.*)/ )
puts "Line2 contains Dogs"
end
执行结果:
Line1 contains Cats
2.1 Regular-Expression Modifiers 修改符
Regular expression literals正则表示字符可以包含可选修改符optional modifier 来控制匹配。修改符在第二个/后,如下 −
Sr.No. | Modifier & Description |
---|---|
1 | i Ignores case when matching text. 忽略大小写 |
2 | o Performs #{} interpolations only once, the first time the regexp literal is evaluated.只执行一次内插,当正则表达式第一次计算时。 |
3 | x Ignores whitespace and allows comments in regular expressions. 忽略空白,允许注释。 |
4 | m Matches multiple lines, recognizing newlines as normal characters. 匹配多行,将新行看作正常字符。 |
5 | u,e,s,n Interprets the regexp as Unicode (UTF-8), EUC, SJIS, or ASCII. If none of these modifiers is specified, the regular expression is assumed to use the source encoding. 将正则表达式分布解释为Unicode, EUC, SJIS,或ASCII码。 |
Ruby 运行regular expressions 以 %r开头,后面跟自选的分隔符。
# Following matches a single slash character, no escape required
%r|/|
# Flag characters are allowed with this syntax, too
%r[</(.*)>]i
2.2 =~
and match
模式匹配可以使用 =~
操作符 或 match 方法
=~
operator
=~
是Ruby 基本模式匹配操作符。 如果发现匹配,返回第一个匹配的索引,否则返回nil。
/hay/ =~ 'haystack' #=> 0
'haystack' =~ /hay/ #=> 0
/a/ =~ 'haystack' #=> 1
/u/ =~ 'haystack' #=> nil
如果匹配,$~
全局变量设置,它是MatchData 对象。. ::last_match等同于 $~
.
match method
match 返回 MatchData 对象。
/st/.match('haystack') #=> #<MatchData "st">
2.3 Metacharacters and Escapes¶ ↑
metacharacters (
, )
, [
, ]
, {
, }
, .
, ?
, +
, *
在模式中出现,有特殊作用。 要匹配它们,需要使用转义符 \. . To match a backslash 例如匹配\,需要使用 \\
.
/1 \+ 2 = 3\?/.match('Does 1 + 2 = 3?') #=> #<MatchData "1 + 2 = 3?">
/a\\\\b/.match('a\\\\b') #=> #<MatchData "a\\b">
/\s\u{6771 4eac 90fd}/.match("Go to 東京都")
#=> #<MatchData " 東京都">
任意 Ruby 表达式可以嵌入模式,使用#{...}结构。
place = "東京都"
/#{place}/.match("Go to 東京都")
#=> #<MatchData "東京都">
2.4 Search and Replace 寻找和替换
最重要的字符串方法是sub 和 gsub, 原地(in-place)对应的是 sub! 和 gsub!.
以上都使用正则表达来执行查找替换。 The sub和 sub! 替换模式中的第一个,而 gsub 和 gsub! 替换所有。
sub 和 gsub 返回新的字符串,原来的字符串不变; 而 sub! 和 gsub! 修改调用的字符串。
demo_subgsub.rb
phone = "2004-959-559 #This is Phone Number"
# Delete Ruby-style comments
phone = phone.sub!(/#.*$/, "")
puts "Phone Num : #{phone}"
# Remove anything other than digits
phone = phone.gsub!(/\D/, "")
puts "Phone Num : #{phone}"
结果:
Phone Num : 2004-959-559
Phone Num : 2004959559
demo_gsub2.rb
text = "rails are rails, really good Ruby on Rails"
# Change "rails" to "Rails" throughout
text.gsub!("rails", "Rails")
# Capitalize the word "Rails" throughout
text.gsub!(/\brails\b/, "Rails")
puts "#{text}"
结果:
Rails are Rails, really good Ruby on Rails
3 Ruby/DBI
Ruby DBI 提供了独立于数据库的界面,类似于Perl DBI 模块。
DBI 提供了Ruby code 和 数据库之间的抽象层。
DBI 支持以下数据库
- ADO (ActiveX Data Objects)
- DB2
- Frontbase
- mSQL
- MySQL
- ODBC
- Oracle
- OCI8 (Oracle)
- PostgreSQL
- Proxy/Server
- SQLite
- SQLRelay
Architecture of a DBI Application DBI 应用架构
Ruby DBI 使用俩层
-
The database interface (DBI) layer. 独立于database,提供常用访问方法,无关通讯的数据库服务器类型。
-
The database driver (DBD) layer. 这层与database相关;不同的数据库引擎有不同的驱动。 MySQL, PostgreSQL, InterBase, Oracle, 等等都有自己的驱动。.每个驱动将解释DBI layer请求,并将它们与给定数据库服务器的请求匹配。
Prerequisites
连接MySQL databases, 需要安装Ruby MySQL 模块。
下载 https://www.tmtm.org/en/mysql/ruby/
备注: 因为 支持数据库的RUBY版面较低,而且 Mysql 等数据库的版本都较低,详细的数据库驱动安装及操作就省略了。