Ruby 简明教程
Ruby - Regular Expressions
正则表达式是一个特殊的字符序列,它帮助您使用保存在模式中的特殊语法匹配或找到其他字符串或一组字符串。
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.
正则表达式文字是在斜杠之间或在任意分隔符之后加 %r 的模式,如下所示 −
A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r as follows −
Syntax
/pattern/
/pattern/im # option can be specified
%r!/usr/local! # general delimited regular expression
Regular-Expression Modifiers
正则表达式文字可能包括一个可选修饰符来控制匹配的各个方面。修饰符在第二个斜杠字符之后指定,如前所示,可用以下字符中的一个表示 −
Regular expression literals may include an optional modifier to control various aspects of matching. The modifier is specified after the second slash character, as shown previously and may be represented by one of these characters −
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. |
与使用 %Q 分隔的字符串文字类似,Ruby 允许你使用 %r 开始你的正则表达式,然后跟上你选择的限定符。当你要描述的模式包含大量你不想要转义的正斜杠字符时,此方法非常有用 −
Like string literals delimited with %Q, Ruby allows you to begin your regular expressions with %r followed by a delimiter of your choice. This is useful when the pattern you are describing contains a lot of forward slash characters that you don’t want to escape −
# Following matches a single slash character, no escape required
%r|/|
# Flag characters are allowed with this syntax, too
%r[</(.*)>]i
Regular-Expression Patterns
除了控制字符 (+ ? . * ^ $ ( ) [ ] { } | \) ,所有字符都匹配自身。你可以在控制字符前面加上反斜杠对其进行转义。
Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash.
Search and Replace
一些最重要的使用正则表达式的 String 方法是 sub 和 gsub ,以及它们的原地变体 sub! 和 gsub! 。
Some of the most important String methods that use regular expressions are sub and gsub, and their in-place variants sub! and gsub!.
所有这些方法都使用 Regexp 模式执行搜索并替换操作。 sub 和 sub! 替换模式的首次出现, gsub 和 gsub! 替换所有出现。
All of these methods perform a search-and-replace operation using a Regexp pattern. The sub & sub! replaces the first occurrence of the pattern and gsub & gsub! replaces all occurrences.
sub 和 gsub 返回一个新字符串,保持原始字符串不变,而 sub! 和 gsub! 修改了调用它们的字符串。
The sub and gsub returns a new string, leaving the original unmodified where as sub! and gsub! modify the string on which they are called.
以下示例 −
Following is the example −
#!/usr/bin/ruby
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}"
这会产生以下结果 −
This will produce the following result −
Phone Num : 2004-959-559
Phone Num : 2004959559
以下示例 −
Following is another example −
#!/usr/bin/ruby
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}"
这会产生以下结果 −
This will produce the following result −
Rails are Rails, really good Ruby on Rails