زبان برنامه نویسی روبی | عبارت های الگودار

سیده آمین ارمان

کاربر نگاه دانلود
کاربر نگاه دانلود
عضویت
2016/05/10
ارسالی ها
1,730
امتیاز واکنش
20,744
امتیاز
795
محل سکونت
البرز
یک عبارت الگودار یک توالی خاص از کارکترهاست که با یک الگو کمک میکند یک رشته یا یک دسته از رشته های به فرم مشخص را پیدا کنیم.


ساختار
یک عبارت الگودار، الگویی است که بین دو علامت اسلش و یا بعد از کاراکترهای %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 contains Cats"
end
if ( line2 =~ /Cats(.*)/ )
puts "Line2 contains Cats"
end

این مثال خروجی زیر را تولید خواهد کرد


Line1 contains Cats

تغییر دهنده ها
عبارتهای الگودار ممکن است تغییر دهنده های مختلفی داشته باشند که ابعاد مختلف انطباق را تغییر می دهند.

تغییر دهنده ها بعد از دومین اسلش قرار میگیرند. فهرست برخی از آن ها در جدول زیر آمده است.


Modifier Description
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.

الگوهای مختلف
بغیر از کاراکترهای کنترلی (+ ? . * ^ $ ( ) [ ] { } | \) همه دیگر کاراکترها بر خودشان انطباق می یابند.

شما می توانید با یک بک اسلش رفتار یک کاراکتر کنترلی را به مانند کاراکترهای عادی تغییر دهید.

فهرست زیر ساختار عبارتهای الگودار قابل استفاده در زبان برنامه نویسی روبی را نشان می دهد.

Pattern Description
^ 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.
 

برخی موضوعات مشابه

بالا