Centos系统下载大全 | Redhat系统下载大全 | Windows2012系统下载大全 | Windows2008系统下载大全| CMS教程 | 网站地图 51运维网-专注Linux/Unix系统安全运维!
当前位置:51运维网 > 编程 > Ruby > 正文

Ruby正则表达式

时间:2014-08-24 22:59 来源:未知 作者:www.51ou.com 阅读:

正则表达式是一个特殊的字符序列可以帮助匹配或者找到其他字符串或串套,使用的模式保持一个专门的语法。

正则表达式文本是一个模式之间的斜线之间或任意分隔符 %r 如下:

语法:

/pattern/
/pattern/im    # option can be specified
%r!/usr/local! # general delimited regular expression  

例如:

#!/usr/bin/ruby

line1 = "Cats are smarter than dogs";
line2 = "Dogs also like meat";

if ( line1 =~ /Cats(.*)/ )
  puts "Line1 starts with Cats"
end
if ( line2 =~ /Cats(.*)/ )
  puts "Line2 starts with Dogs"
end  

这将产生以下结果:

Line1 starts with Cats  

正则表达式修饰符:

正则表达式的文字可以包括一个可选的修饰符来控制各方面的匹配。修改指定第二个斜杠字符后,如前面所示,可表示为这些字符之一:

修饰符 描述
i Ignore case when matching text.
o Perform #{} interpolations only once, the first time the regexp literal is evaluated.
x Ignores whitespace and allows comments in regular expressions
m Matches multiple lines, recognizing newlines as normal characters
u,e,s,n Interpret 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,然后由所选择的定界符。这是非常有用的,当所描述的模式中包含正斜杠字符不希望转义:

# Following matches a single slash character, no escape required
%r|/|               

# Flag characters are allowed with this syntax, too
%r[</(.*)>]i    

正则表达式模式:

除控制字符, (+ ? . * ^ $ ( ) [ ] { } | \), 所有字符匹配。可以转义控制字符前面加上反斜线。

下表列出了可在Ruby的​​正则表达式语法。

模式 描述
^ Matches beginning of line.
$ Matches end of line.
. Matches any single character except newline. Using m option allows it to match newline as well.
[...] Matches any single character in brackets.
[^...] Matches any single character not in brackets
re* Matches 0 or more occurrences of preceding expression.
re+ Matches 1 or more occurrence of preceding expression.
re? Matches 0 or 1 occurrence of preceding expression.
re{ n} Matches exactly n number of occurrences of preceding expression.
re{ n,} Matches n or more occurrences of preceding expression.
re{ n, m} Matches at least n and at most m occurrences of preceding expression.
a| b Matches either a or b.
(re) Groups regular expressions and remembers matched text.
(?imx) Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected.
(?-imx) Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected.
(?: re) Groups regular expressions without remembering matched text.
(?imx: re) Temporarily toggles on i, m, or x options within parentheses.
(?-imx: re) Temporarily toggles off i, m, or x options within parentheses.
(?#...) Comment.
(?= re) Specifies position using a pattern. Doesn't have a range.
(?! re) Specifies position using pattern negation. Doesn't have a range.
(?> re) Matches independent pattern without backtracking.
\w Matches word characters.
\W Matches nonword characters.
\s Matches whitespace. Equivalent to [\t\n\r\f].
\S Matches nonwhitespace.
\d Matches digits. Equivalent to [0-9].
\D Matches nondigits.
\A Matches beginning of string.
\Z Matches end of string. If a newline exists, it matches just before newline.
\z Matches end of string.
\G Matches point where last match finished.
\b Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
\B Matches nonword boundaries.
\n, \t, etc. Matches newlines, carriage returns, tabs, etc.
\1...\9 Matches nth grouped subexpression.
\10 Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code.

正则表达式的例子:

文字字符:

例子 描述
/ruby/ Match "ruby".
¥ Matches Yen sign. Multibyte characters are suported in Ruby 1.9 and Ruby 1.8.

字符类:

例子 描述
/[Rr]uby/ Match "Ruby" or "ruby"
/rub[ye]/ Match "ruby" or "rube"
/[aeiou]/ Match any one lowercase vowel
/[0-9]/ Match any digit; same as /[0123456789]/
/[a-z]/ Match any lowercase ASCII letter
/[A-Z]/ Match any uppercase ASCII letter
/[a-zA-Z0-9]/ Match any of the above
/[^aeiou]/ Match anything other than a lowercase vowel
/[^0-9]/ Match anything other than a digit

特殊字符类:

例子 描述
/./ Match any character except newline
/./m In multiline mode . matches newline, too
/\d/ Match a digit: /[0-9]/
/\D/ Match a nondigit: /[^0-9]/
/\s/ Match a whitespace character: /[ \t\r\n\f]/
/\S/ Match nonwhitespace: /[^ \t\r\n\f]/
/\w/ Match a single word character: /[A-Za-z0-9_]/
/\W/ Match a nonword character: /[^A-Za-z0-9_]/

重复例:

例子 描述
/ruby?/ Match "rub" or "ruby": the y is optional
/ruby*/ Match "rub" plus 0 or more ys
/ruby+/ Match "rub" plus 1 or more ys
/\d{3}/ Match exactly 3 digits
/\d{3,}/ Match 3 or more digits
/\d{3,5}/ Match 3, 4, or 5 digits

不再是无限制的重复:

此相匹配的最小的重复次数:

例子 描述
/<.*>/ Greedy repetition: matches "<ruby>perl>"
/<.*?>/ Nongreedy: matches "<ruby>" in "<ruby>perl>"

用括号分组:

例子 描述
/\D\d+/ No group: + repeats \d
/(\D\d)+/ Grouped: + repeats \D\d pair
/([Rr]uby(, )?)+/ Match "Ruby", "Ruby, ruby, ruby", etc.

感谢您对【51运维网 http://www.51ou.com/】的支持,我们为您免费提供《Ruby正则表达式》技术文章,《Ruby正则表达式》详细使用和说明,有时《Ruby正则表达式》可能不完善、敬请谅解!如果《Ruby正则表达式》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

顶一下
(0)
0%
踩一下
(0)
0%
上一篇:Ruby面向对象
下一篇:Ruby MySQL DBI实例
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码:点击我更换图片