Ruby/Справочник/Regexp

Материал из Викиучебника — открытых книг для открытого мира
Перейти к навигации Перейти к поиску

Класс Regexp[править]

Документ-класс: Regexp Regexp содержит регулярные выражения, используется для сопоставления шаблона с строками. Регулярные выражения создаются с помощью /.../ и %r{...} знаков, а также с конструктором Regexp::new.


Константы

EXTENDED, IGNORECASE, MULTILINE

Методы класса

compile, escape, last_match, new, quote, union, yaml_new

Методы объекта

&, ===, ==, =~, casefold?, eql?, hash, inspect, kcode, match, options, source, to_s, to_yaml, ||, ~

Regexp::compile[править]


 Regexp::compile(...)

Синоним для Regexp.new

Regexp::escape[править]


 Regexp.escape(str)   => a_str
 Regexp.quote(str)    => a_str

Вытесняет любые символы, которые имеют особое значение в регулярном выражении. Возвращает новую экранированную строку или self, если никакие символы не экранированы. Для любой строки, Regexp.escape(str)=~str будет true.

  Regexp.escape('\*?{}.')   #=> \\*\?\{\}\.

Regexp::last_match[править]


 Regexp.last_match           => matchdata
 Regexp.last_match(fixnum)   => str

The first form returns the MatchData object generated by the last successful pattern match. Equivalent to reading the global variable $~. The second form returns the nth field in this MatchData object.

  /c(.)t/ =~ 'cat'       #=> 0
  Regexp.last_match      #=> #<MatchData:0x401b3d30>
  Regexp.last_match(0)   #=> "cat"
  Regexp.last_match(1)   #=> "a"
  Regexp.last_match(2)   #=> nil

Regexp::new[править]


 Regexp.new(string [, options [, lang]])       => regexp
 Regexp.new(regexp)                            => regexp
 Regexp.compile(string [, options [, lang]])   => regexp
 Regexp.compile(regexp)                        => regexp

Constructs a new regular expression from pattern, which can be either a String or a Regexp (in which case that regexp's options are propagated, and new options may not be specified (a change as of Ruby 1.8). If options is a Fixnum, it should be one or more of the constants Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE, or-ed together. Otherwise, if options is not nil, the regexp will be case insensitive. The lang parameter enables multibyte support for the regexp: `n', `N' = none, `e', `E' = EUC, `s', `S' = SJIS, `u', `U' = UTF-8.

  r1 = Regexp.new('^a-z+:\s+\w+')           #=> /^a-z+:\s+\w+/
  r2 = Regexp.new('cat', true)               #=> /cat/i
  r3 = Regexp.new('dog', Regexp::EXTENDED)   #=> /dog/x
  r4 = Regexp.new(r2)                        #=> /cat/i

Regexp::quote[править]


 Regexp.escape(str)   => a_str
 Regexp.quote(str)    => a_str

Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string, or self if no characters are escaped. For any string, Regexp.escape(str)=~str will be true.

  Regexp.escape('\*?{}.')   #=> \\*\?\{\}\.

Regexp::union[править]


 Regexp.union([pattern]*)   => new_str

Return a Regexp object that is the union of the given patterns, i.e., will match any of its parts. The patterns can be Regexp objects, in which case their options will be preserved, or Strings. If no arguments are given, returns /(?!)/.

  Regexp.union                         #=> /(?!)/
  Regexp.union("penzance")             #=> /penzance/
  Regexp.union("skiing", "sledding")   #=> /skiing|sledding/
  Regexp.union(/dogs/, /cats/i)        #=> /(?-mix:dogs)|(?i-mx:cats)/

Regexp::yaml_new[править]


 Regexp::yaml_new( klass, tag, val )

(нет описания...)

Regexp#&[править]


 &(other)

(нет описания...)

Regexp#==[править]


 rxp == other_rxp      => true or false
 rxp.eql?(other_rxp)   => true or false

Equality---Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same.

  /abc/  == /abc/x   #=> false
  /abc/  == /abc/i   #=> false
  /abc/u == /abc/n   #=> false

Regexp#===[править]


 rxp === str   => true or false

Case Equality---Synonym for Regexp#=~ used in case statements.

  a = "HELLO"
  case a
  when /^[a-z]*$/; print "Lower case\n"
  when /^[A-Z]*$/; print "Upper case\n"
  else;            print "Mixed case\n"
  end

produces:

  Upper case

Regexp#=~[править]


 rxp.match(str)   => matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match. This is equivalent to retrieving the value of the special variable $~ following a normal match.

  /(.)(.)(.)/.match("abc")[2]   #=> "b"

Regexp#casefold?[править]


 rxp.casefold?   => true or false

Returns the value of the case-insensitive flag.

Regexp#eql?[править]


 rxp == other_rxp      => true or false
 rxp.eql?(other_rxp)   => true or false

Equality---Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same.

  /abc/  == /abc/x   #=> false
  /abc/  == /abc/i   #=> false
  /abc/u == /abc/n   #=> false

Regexp#hash[править]


 rxp.hash   => fixnum

Produce a hash based on the text and options of this regular expression.

Regexp#inspect[править]


 rxp.inspect   => string

Produce a nicely formatted string-version of rxp. Perhaps surprisingly, #inspect actually produces the more natural version of the string than #to_s.

   /ab+c/ix.to_s         #=> /ab+c/ix

Regexp#kcode[править]


 rxp.kcode   => str

Returns the character set code for the regexp.

Regexp#match[править]


 rxp.match(str)   => matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match. This is equivalent to retrieving the value of the special variable $~ following a normal match.

  /(.)(.)(.)/.match("abc")[2]   #=> "b"

Regexp#options[править]


 rxp.options   => fixnum

Returns the set of bits corresponding to the options used when creating this Regexp (see Regexp::new for details. Note that additional bits may be set in the returned options: these are used internally by the regular expression code. These extra bits are ignored if the options are passed to Regexp::new.

  Regexp::IGNORECASE                  #=> 1
  Regexp::EXTENDED                    #=> 2
  Regexp::MULTILINE                   #=> 4
  /cat/.options                       #=> 128
  /cat/ix.options                     #=> 131
  Regexp.new('cat', true).options     #=> 129
  Regexp.new('cat', 0, 's').options   #=> 384
  r = /cat/ix
  Regexp.new(r.source, r.options)     #=> /cat/ix

Regexp#source[править]


 rxp.source   => str

Returns the original string of the pattern.

  /ab+c/ix.source   #=> "ab+c"

Regexp#to_s[править]


 rxp.to_s   => str

Returns a string containing the regular expression and its options (using the (?xxx:yyy) notation. This string can be fed back in to Regexp::new to a regular expression with the same semantics as the original. (However, Regexp#== may not return true when comparing the two, as the source of the regular expression itself may differ, as the example shows). Regexp#inspect produces a generally more readable version of rxp.

  r1 = /ab+c/ix         #=> /ab+c/ix
  s1 = r1.to_s          #=> "(?ix-m:ab+c)"
  r2 = Regexp.new(s1)   #=> /(?ix-m:ab+c)/
  r1 == r2              #=> false
  r1.source             #=> "ab+c"
  r2.source             #=> "(?ix-m:ab+c)"

Regexp#to_yaml[править]


 to_yaml( opts = {} )

(нет описания...)

Regexp#|[править]


 |(other)

(нет описания...)

Regexp#~[править]


 ~ rxp   => integer or nil

Match---Matches rxp against the contents of $_. Equivalent to rxp =~ $_.

  $_ = "input data"
  ~ /at/   #=> 7