添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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"
if ( line2 =~ /Cats(.*)/ )
   puts "Line2 contains  Dogs"

执行结果:

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 等数据库的版本都较低,详细的数据库驱动安装及操作就省略了。

Ruby是一个面向对象的脚本语言,由Yukihiro Matsumoto设计。它可以运行于各种平台,Windows,Mac OS, 及各种Linux/Unix。 Ruby特点: 开源,授权 通用解释性语言 服务器端的脚本语言,和Python, Perl类似 可以来写CGI(common gateway i... Ruby 数值是有序的,整数为索引的任何对象集合。数组中的每个元素与索引关联使用。 和C或Java 一样, 数组索引从0开始。负的索引被看作从数组尾部开始。-1代表数组最后的元素,-2代表倒数第二个元素,等等。 Ruby 数组可以容纳String, Intege... Ruby 在kernel模块实现了I/O相关的方法。这些I/O 方法都是 IO 类派生的。 IO 类提供了 基本方法:read, write, gets, puts, readline, getc,andprintf. 本节介绍这些基本方...
eclipse下RDT插件下载网 http://yehudakatz.com/2007/01/27/my-preferred-rails-development-environment/ http://beans.seartipy.com/2006/08/12/develop-ruby-applications-using-eclipse-ide/ ruby安装版的: http...
转知乎 作者:@萧井陌, @Badger启蒙 完成Codecademy 的 Python 部分。这只是热身部分,尽快完成它,因为你永远只是在浏览器里,你不会学到如何搭建开发环境。在 Codecademy 这类的编程学习网站学到的那点儿东西,哪怕你只想做一个小的不能再小的项目,你都不知道该从哪儿开始。 完成 MIT 计算机导论课(如果你英语不过关:麻省理工学院公开课:计算机科学及编程导论)。MOOC
源参考链接:https://github.com/crane-yuan/free-programming-books/blob/master/free-programming-books-zh.md IDEMySQLNoSQLPostgreSQLWebWEB服务器其它函数式概念分布式系统在线教育大数据操作系统数据库智能系统正则表达式版本控制程序员杂谈管理和监控编程