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

Anchors

By default, a regular expression will try to find the first match for the pattern in a string. Match /iss/against the string ``Mississippi,'' and it will find the substring ``iss'' starting at position one. But what if you want to force a pattern to match only at the start or end of a string?

The patterns ^and$match the beginning and end of a line, respectively. These are often used toanchora pattern match: for example,/^option/matches the word ``option'' only if it appears at the start of a line. The sequence\Amatches the beginning of a string, and\zand\Zmatch the end of a string. (Actually,\Zmatches the end of a stringunlessthe string ends with a ``\n'', it which case it matches just before the ``\n''.)

showRE("this is\nthe time", /^the/)

»

this is\n<<the>> time

showRE("this is\nthe time", /is$/)

»

this <<is>>\nthe time

showRE("this is\nthe time", /\Athis/)

»

<<this>> is\nthe time

showRE("this is\nthe time", /\Athe/)

»

no match

Similarly, the patterns \band\Bmatch word boundaries and nonword boundaries, respectively. Word characters are letters, numbers, and underscore.

showRE("this is\nthe time", /\bis/)

»

this <<is>>\nthe time

showRE("this is\nthe time", /\Bis/)

»

th<<is>> is\nthe time

Character Classes

A character class is a set of characters between brackets: [characters]matches any single character between the brackets.[aeiou]will match a vowel,[,.:;!?]matches punctuation, and so on. The significance of the special regular expression characters---.|()[{+^$*?---is turned off inside the brackets. However, normal string substitution still occurs, so (for example)\brepresents a backspace character and\na newline (see Table 18.2 on page 203). In addition, you can use the abbreviations shown in Table 5.1 on page 59, so that (for example)\smatches any whitespace character, not just a literal space.

showRE('It costs $12.', /[aeiou]/)

»

It c<<o>>sts $12.

showRE('It costs $12.', /[\s]/)

»

It<< >>costs $12.

Within the brackets, the sequence c1-c2represents all the characters between c1and c2, inclusive.

If you want to include the literal characters ]and-within a character class, they must appear at the start.

a = 'Gamma [Design Patterns-page 123]'

showRE(a, /[]]/)

»

Gamma [Design Patterns-page 123<<]>>

showRE(a, /[B-F]/)

»

Gamma [<<D>>esign Patterns-page 123]

showRE(a, /[-]/)

»

Gamma [Design Patterns<<->>page 123]

showRE(a, /[0-9]/)

»

Gamma [Design Patterns-page <<1>>23]

Put a ^immediately after the opening bracket to negate a character class:[^a-z]matches any character that isn't a lowercase alphabetic.

Some character classes are used so frequently that Ruby provides abbreviations for them. These abbreviations are listed in Table 5.1 on page 59---they may be used both within brackets and in the body of a pattern.

showRE('It costs $12.', /\s/)

»

It<< >>costs $12.

showRE('It costs $12.', /\d/)

»

It costs $<<1>>2.

Character class abbreviations

Sequence

As [ ... ]

Meaning

\d

[0-9]

Digit character

\D

[^0-9]

Nondigit

\s

[\s\t\r\n\f]

Whitespace character

\S

[^\s\t\r\n\f]

Nonwhitespace character

\w

[A-Za-z0-9_]

Word character

\W

[^A-Za-z0-9_]

Nonword character

Finally, a period (``.'') appearing outside brackets represents any character except a newline (and in multiline mode it matches a newline, too).

a = 'It costs $12.'

showRE(a, /c.s/)

»

It <<cos>>ts $12.

showRE(a, /./)

»

<<I>>t costs $12.

showRE(a, /\./)

»

It costs $12<<.>>

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