Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
122
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Substitutions

#{...}

Performs an expression substitution, as with strings. By default, the substitution is performed each time a regular expression literal is evaluated. With the /ooption, it is performed just the first time.

\0, \1, \2, ... \9, \&, \`, \', \+

Substitutes the value matched by the nth grouped subexpression, or by the entire match, pre- or postmatch, or the highest group.

Extensions

In common with Perl and Python, Ruby regular expressions offer some extensions over traditional Unix regular expressions. All the extensions are entered between the characters (?and). The parentheses that bracket these extensions are groups, but they do not generate backreferences: they do not set the values of\1and$1etc.

(?# comment)

Inserts a comment into the pattern. The content is ignored during pattern matching.

(?:re)

Makes reinto a group without generating backreferences. This is often useful when you need to group a set of constructs but don't want the group to set the value of$1or whatever. In the example that follows, both patterns match a date with either colons or spaces between the month, day, and year. The first form stores the separator character in$2and$4, while the second pattern doesn't store the separator in an external variable.

date = "12/25/01"

date =~ %r{(\d+)(/|:)(\d+)(/|:)(\d+)}

[$1,$2,$3,$4,$5]

»

["12", "/", "25", "/", "01"]

date =~ %r{(\d+)(?:/|:)(\d+)(?:/|:)(\d+)}

[$1,$2,$3]

»

["12", "25", "01"]

(?=re)

Matches reat this point, but does not consume it (also known charmingly as ``zero-width positive lookahead''). This lets you look forward for the context of a match without affecting$&. In this example, thescanmethod matches words followed by a comma, but the commas are not included in the result.

str = "red, white, and blue"

str.scan(/[a-z]+(?=,)/)

»

["red", "white"]

(?!re)

Matches if redoes not match at this point. Does not consume the match (zero-width negative lookahead). For example,/hot(?!dog)(\w+)/matches any word that contains the letters ``hot'' that aren't followed by ``dog'', returning the end of the word in$1.

(?>re)

Nests an independent regular expression within the first regular expression. This expression is anchored at the current match position. If it consumes characters, these will no longer be available to the higher-level regular expression. This construct therefore inhibits backtracking, which can be a performance enhancement. For example, the pattern /a.*b.*a/takes exponential time when matched against a string containing an ``a'' followed by a number of ``b''s, but with no trailing ``a.'' However, this can be avoided by using a nested regular expression/a(?>.*b).*a/. In this form, the nested expression consumes all the the input string up to the last possible ``b'' character. When the check for a trailing ``a'' then fails, there is no need to backtrack, and the pattern match fails promptly.

require "benchmark"

include Benchmark

str = "a" + ("b" * 5000)

bm(8) do |test|

  test.report("Normal:") { str =~ /a.*b.*a/ }

  test.report("Nested:") { str =~ /a(?>.*b).*a/ }

end

produces:

              user     system      total        real

Normal:   4.170000   0.000000   4.170000 (  4.150970)

Nested:   0.000000   0.000000   0.000000 (  0.001529)

(?imx)

Turns on the corresponding ``i,'' ``m,'' or ``x'' option. If used inside a group, the effect is limited to that group.

(?-imx)

Turns off the ``i,'' ``m,'' or ``x'' option.

(?imx:re)

Turns on the ``i,'' ``m,'' or ``x'' option for re.

(?-imx:re)

Turns off the ``i,'' ``m,'' or ``x'' option for re.

Names

Ruby names are used to refer to constants, variables, methods, classes, and modules. The first character of a name helps Ruby to distinguish its intended use. Certain names, listed in Table 18.3 on page 210, are reserved words and should not be used as variable, method, class, or module names.

Reserved words

__FILE__

and

def

end

in

or

self

unless

__LINE__

begin

defined?

ensure

module

redo

super

until

BEGIN

break

do

false

next

rescue

then

when

END

case

else

for

nil

retry

true

while

alias

class

elsif

if

not

return

undef

yield

In these descriptions, lowercase lettermeans the characters ``a'' though ``z'', as well as ``_'', the underscore.Uppercase lettermeans ``A'' though ``Z,'' anddigitmeans ``0'' through ``9.''Name charactersmeans any combination of upper- and lowercase letters and digits.

A local variable name consists of a lowercase letter followed by name characters.

fred  anObject  _x  three_two_one

An instance variable name starts with an ``at'' sign (``@'') followed by an upper- or lowercase letter, optionally followed by name characters.

@name  @_  @Size

A class variable name starts with two ``at'' signs (``@@'') followed by an upper- or lowercase letter, optionally followed by name characters.

@@name  @@_  @@Size

A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout.

module Math

  PI = 3.1415926

end

class BigBlob

Global variables, and some special system variables, start with a dollar sign (``$'') followed by name characters. In addition, there is a set of two-character variable names in which the second character is a punctuation character. These predefined variables are listed starting on page 213. Finally, a global variable name can be formed using ``$-'' followed byanysingle character.

$params  $PROGRAM  $!  $_  $-a  $-.

Method names are described in the section beginning on page 225.

Соседние файлы в папке Ruby