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


5119
5120
5121
5122
5123
# File 'lib/source/ruby.rb', line 5119

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


5064
5065
5066
# File 'lib/source/ruby.rb', line 5064

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('\\*?{}.')   #=> \\\\\*\?\{\}\.


5078
5079
5080
# File 'lib/source/ruby.rb', line 5078

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

.last_matchObject

FIX: Incomplete



5083
5084
# File 'lib/source/ruby.rb', line 5083

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('\\*?{}.')   #=> \\\\\*\?\{\}\.


5096
5097
5098
# File 'lib/source/ruby.rb', line 5096

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

.unionObject

FIX: Incomplete



5101
5102
# File 'lib/source/ruby.rb', line 5101

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


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

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



5155
5156
5157
5158
5159
5160
5161
# File 'lib/source/ruby.rb', line 5155

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



5173
5174
5175
5176
5177
5178
5179
# File 'lib/source/ruby.rb', line 5173

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)


5190
5191
5192
# File 'lib/source/ruby.rb', line 5190

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)


5204
5205
5206
# File 'lib/source/ruby.rb', line 5204

def eql?(rxp)
  `this.__source__===rxp.__source__&&this.__options__===rxp.__options__`
end

#hashObject

:nodoc:



5208
5209
# File 'lib/source/ruby.rb', line 5208

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


5218
5219
5220
# File 'lib/source/ruby.rb', line 5218

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"


5231
5232
5233
5234
5235
5236
5237
5238
5239
# File 'lib/source/ruby.rb', line 5231

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


5259
5260
5261
5262
5263
5264
5265
# File 'lib/source/ruby.rb', line 5259

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"


5274
5275
5276
# File 'lib/source/ruby.rb', line 5274

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)"


5287
5288
5289
5290
# File 'lib/source/ruby.rb', line 5287

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



5182
5183
# File 'lib/source/ruby.rb', line 5182

def ~
end