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


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


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


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



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

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


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

# Flag characters are allowed with this syntax, too


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


模式 描述
^ 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正则表达式》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

下一篇:Ruby MySQL DBI实例