Section 3.5. Using Quantifiers


3.4. Using Anchors

An anchor is a special expression that matches a position in a string rather than a character or sequence of characters. As we'll see later, this is a simple case of a zero-width assertion, a match that doesn't consume any of the string when it matches.

The most common anchors were already listed at the beginning of this chapter. The simplest are ^ and $, which match the beginning and end of the string.

string  = "abcXdefXghi" /def/  =~ string     # 4 /abc/  =~ string     # 0 /ghi/  =~ string     # 8 /^def/ =~ string     # nil /def$/ =~ string     # nil /^abc/ =~ string     # 0 /ghi$/ =~ string     # 8


However, I've told a small lie. These anchors don't actually match the beginning and end of the string but of the line. Consider the same patterns applied to a similar string with embedded newlines:

string  = "abc\ndef\nghi" /def/  =~ string     # 4 /abc/  =~ string     # 0 /ghi/  =~ string     # 8 /^def/ =~ string     # 4 /def$/ =~ string     # 4 /^abc/ =~ string     # 0 /ghi$/ =~ string     # 8


However, we also have the special anchors \A and \Z, which match the real beginning and end of the string itself.

string  = "abc\ndef\nghi" /\Adef/ =~ string    # nil /def\Z/ =~ string    # nil /\Aabc/ =~ string    # 0 /ghi\Z/ =~ string    # 8


The \z is the same as \Z except that the latter matches before a terminating newline, whereas the former must match explicitly.

string  = "abc\ndef\nghi" str2 << "\n" /ghi\Z/ =~ string    # 8 /\Aabc/ =~ str2      # 8 /ghi\z/ =~ string    # 8 /ghi\z/ =~ str2      # nil


It's also possible to match a word boundary with \b, or a position that is not a word boundary with \B. These gsub examples make it clear how this works:

str = "this is a test" str.gsub(/\b/,"|")      # "|this| |is| |a| |test|" str.gsub(/\B/,"-")      # "t-h-i-s i-s a t-e-s-t"


There is no way to distinguish between beginning and ending word boundaries.




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net