Class: Regexp

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

A Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the /.../ and %r{...} literals or the Regexp::new constructor.

In Red, as in Ruby, ^ and $ always match before and after newlines – this is the equivalent of JavaScript’s m flag being always turned on – so matching at the start or the end of a string is accomplished using \A and \Z. Ruby’s m flag, the equivalent of an s flag in Perl (“dot matches newline”), is not supported by JavaScript; to match any character including newlines, use [\s\S] in place of dot (.). Red does not currently support the x flag (“ignore whitespace and allow comments”), the o flag (“evaluate regexp once”), or the s, u, n, and e character-encoding flags.

Constant Summary collapse

IGNORECASE =
1
EXTENDED =
2
MULTILINE =
4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, options) ⇒ Regexp

call-seq:

Regexp.compile(string, ignore_case = false) -> regexp
Regexp.compile(regexp)                      -> regexp
Regexp.new(string, ignore_case = false)     -> regexp
Regexp.new(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). Red currently supports only the i option flag. (See class-level documentation for details.)

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


5071
5072
5073
5074
5075
# File 'lib/source/ruby.rb', line 5071

def initialize(regexp, options)
  `switch(options){case 0:this._options='';break;case 1:this._options='i';break;case 2:this._options='x';break;case 3:this._options='ix';break;case 4:this._options='s';break;case 5:this._options='si';break;case 6:this._options='sx';break;case 7:this._options='six';break;default:this._options=options?'i':'';}`
  `this._source=regexp._value||regexp`
  `this._value=new(RegExp)(this._source,'m'+(/i/.test(this._options)?'i':''))`
end

Class Method Details

.compile(value, options) ⇒ Object

call-seq:

Regexp.compile(string, ignore_case = false) -> regexp
Regexp.compile(regexp)                      -> regexp
Regexp.new(string, ignore_case = false)     -> regexp
Regexp.new(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). Red currently supports only the i option flag. (See class-level documentation for details.)

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


5016
5017
5018
# File 'lib/source/ruby.rb', line 5016

def self.compile(value,options)
  Regexp.new(value,options)
end

.escape(str) ⇒ Object

call-seq:

Regexp.escape(str) -> string
Regexp.quote(str)  -> string

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

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


5030
5031
5032
# File 'lib/source/ruby.rb', line 5030

def self.escape(str)
  `$q(str._value.replace(/([-.*+?^${}()|[\\]\\/\\\\])/g, '\\\\$1'))`
end

.last_matchObject

FIX: Incomplete



5035
5036
# File 'lib/source/ruby.rb', line 5035

def self.last_match
end

.quote(str) ⇒ Object

call-seq:

Regexp.escape(str) -> string
Regexp.quote(str)  -> string

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

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


5048
5049
5050
# File 'lib/source/ruby.rb', line 5048

def self.quote(str)
  `str._value.replace(/([-.*+?^${}()|[\\]\\/\\\\])/g, '\\\\$1')`
end

.unionObject

FIX: Incomplete



5053
5054
# File 'lib/source/ruby.rb', line 5053

def self.union
end

Instance Method Details

#==(rxp) ⇒ Object

call-seq:

rxp == other    -> true or false
rxp.eql?(other) -> true or false

Equality – two regexps are equal if their patterns are identical and their IGNORECASE values are the same.

/abc/ == /abc/    #=> true
/abc/ == /abc/i   #=> false


5087
5088
5089
# File 'lib/source/ruby.rb', line 5087

def ==(rxp)
  `this._source===rxp._source&&this._options===rxp._options`
end

#===(string) ⇒ Object

call-seq:

rxp === str -> true or false

Case Equality – synonym for Regexp#=~ used in case statements.

case "HELLO"
  when /^[a-z]*$/ : puts "Lower case"
  when /^[A-Z]*$/ : puts "Upper case"
  else              puts "Mixed case"
end

produces:

Upper case

FIX: Incomplete



5107
5108
5109
5110
5111
5112
5113
# File 'lib/source/ruby.rb', line 5107

def ===(string)
  `var c=$u,result=c$MatchData.m$new()`
  `if(!$T(c=string._value.match(this._value))){return nil;}`
  `for(var i=0,l=c.length;i<l;++i){result._captures[i]=$q(c[i])}`
  `result._string=string._value`
  return `result`
end

#=~(string) ⇒ Object

call-seq:

rxp =~ str     -> matchdata or nil
rxp.match(str) -> matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match.

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

FIX: Incomplete



5125
5126
5127
5128
5129
5130
5131
# File 'lib/source/ruby.rb', line 5125

def =~(string)
  `var c=$u,result=c$MatchData.m$new()`
  `if(!$T(c=string._value.match(this._value))){return nil;}`
  `for(var i=0,l=c.length;i<l;++i){result._captures[i]=$q(c[i])}`
  `result._string=string._value`
  return `result`
end

#casefold?Boolean

call-seq:

rxp.casefold? -> true or false

Returns the status of the IGNORECASE flag.

Returns:

  • (Boolean)


5142
5143
5144
# File 'lib/source/ruby.rb', line 5142

def casefold?
  `/i/.test(this._options)`
end

#eql?(rxp) ⇒ Boolean

call-seq:

rxp == other    -> true or false
rxp.eql?(other) -> true or false

Equality – two regexps are equal if their patterns are identical and their IGNORECASE values are the same.

/abc/.eql? /abc/    #=> true
/abc/.eql? /abc/i   #=> false

Returns:

  • (Boolean)


5156
5157
5158
# File 'lib/source/ruby.rb', line 5156

def eql?(rxp)
  `this._source===rxp._source&&this._options===rxp._options`
end

#hashObject

:nodoc:



5160
5161
# File 'lib/source/ruby.rb', line 5160

def hash # :nodoc:
end

#inspectObject

call-seq:

rxp.inspect -> string

Returns a representation of rxp as a Regexp literal.

/ab+c/i.inspect   #=> /ab+c/i


5170
5171
5172
# File 'lib/source/ruby.rb', line 5170

def inspect
  `$q(''+this)`
end

#match(string) ⇒ Object

call-seq:

rxp =~ str     -> matchdata or nil
rxp.match(str) -> matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match.

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


5183
5184
5185
5186
5187
5188
5189
5190
5191
# File 'lib/source/ruby.rb', line 5183

def match(string)
  `var c=$u,result=c$MatchData.m$new()`
  `if(!$T(c=string._value.match(this._value))){return nil;}`
  `for(var i=0,l=c.length;i<l;++i){result._captures[i]=$q(c[i])}`
  `result._string=string._value`
  `result._pre=RegExp.leftContext`
  `result._post=RegExp.rightContext`
  return `result`
end

#optionsObject

call-seq:

rxp.options -> num

Returns the set of bits corresponding to the options used when creating this Regexp. Red currently supports only the i option flag. (See Regexp::new for details.)

Regexp::IGNORECASE                #=> 1
Regexp::EXTENDED                  #=> 2
Regexp::MULTILINE                 #=> 4

/cat/.options                     #=> 0
/cat/i.options                    #=> 1
Regexp.new('cat', true).options   #=> 1

r = /cat/i
Regexp.new(r.source, r.options)   #=> /cat/i


5211
5212
5213
5214
5215
5216
5217
# File 'lib/source/ruby.rb', line 5211

def options
  `var result=0`
  `if(/i/.test(this._options)){result+=1}`
  `if(/x/.test(this._options)){result+=2}`
  `if(/s/.test(this._options)){result+=4}`
  return `result`
end

#sourceObject

call-seq:

rxp.source -> str

Returns the original string of the pattern.

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


5226
5227
5228
# File 'lib/source/ruby.rb', line 5226

def source
  `$q(this._source)`
end

#to_sObject

call-seq:

rxp.to_s -> str

Returns a string containing the regular expression and its options (using the (?xxx:yyy) notation. Regexp#inspect produces a generally more readable version of rxp.

/ab+c/i.to_s   #=> "(?i-mx:ab+c)"


5239
5240
5241
5242
# File 'lib/source/ruby.rb', line 5239

def to_s
  `var o=this._options.replace('s','m'),c=o.match(/(m)?(i)?(x)?/)`
  `$q('(?'+o+(c[0]=='mix'?'':'-')+(c[1]?'':'m')+(c[2]?'':'i')+(c[3]?'':'x')+':'+this._source+')')`
end

#~Object

FIX: Incomplete



5134
5135
# File 'lib/source/ruby.rb', line 5134

def ~
end