Class: Regexp
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
-
.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.
-
.escape ⇒ Object
FIX: Incomplete.
-
.last_match ⇒ Object
FIX: Incomplete.
-
.quote ⇒ Object
FIX: Incomplete.
-
.union ⇒ Object
FIX: Incomplete.
Instance Method Summary collapse
-
#==(rxp) ⇒ Object
call-seq: rxp == other -> true or false rxp.eql?(other) -> true or false.
-
#===(string) ⇒ Object
call-seq: rxp === str -> true or false.
-
#=~(string) ⇒ Object
call-seq: rxp =~ str -> matchdata or nil rxp.match(str) -> matchdata or nil.
-
#casefold? ⇒ Boolean
call-seq: rxp.casefold? -> true or false.
-
#eql?(rxp) ⇒ Boolean
call-seq: rxp == other -> true or false rxp.eql?(other) -> true or false.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(regexp, options) ⇒ Regexp
constructor
call-seq: Regexp.compile(string, ignore_case = false) -> regexp Regexp.compile(regexp) -> regexp Regexp.new(string, ignore_case = false) -> regexp Regexp.new(regexp) -> regexp.
-
#inspect ⇒ Object
call-seq: rxp.inspect -> string.
-
#match(string) ⇒ Object
call-seq: rxp =~ str -> matchdata or nil rxp.match(str) -> matchdata or nil.
-
#options ⇒ Object
call-seq: rxp.options -> num.
-
#source ⇒ Object
call-seq: rxp.source -> str.
-
#to_s ⇒ Object
call-seq: rxp.to_s -> str.
-
#~ ⇒ Object
FIX: Incomplete.
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
5038 5039 5040 5041 5042 |
# File 'lib/source/ruby.rb', line 5038 def initialize(regexp, ) `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
5003 5004 5005 |
# File 'lib/source/ruby.rb', line 5003 def self.compile(value,) Regexp.new(value,) end |
.escape ⇒ Object
FIX: Incomplete
5008 5009 |
# File 'lib/source/ruby.rb', line 5008 def self.escape end |
.last_match ⇒ Object
FIX: Incomplete
5012 5013 |
# File 'lib/source/ruby.rb', line 5012 def self.last_match 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
5054 5055 5056 |
# File 'lib/source/ruby.rb', line 5054 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
5074 5075 5076 5077 5078 5079 5080 |
# File 'lib/source/ruby.rb', line 5074 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
5092 5093 5094 5095 5096 5097 5098 |
# File 'lib/source/ruby.rb', line 5092 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.
5109 5110 5111 |
# File 'lib/source/ruby.rb', line 5109 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
5123 5124 5125 |
# File 'lib/source/ruby.rb', line 5123 def eql?(rxp) `this._source===rxp._source&&this._options===rxp._options` end |
#inspect ⇒ Object
call-seq:
rxp.inspect -> string
Returns a representation of rxp as a Regexp literal.
/ab+c/i.inspect #=> /ab+c/i
5137 5138 5139 |
# File 'lib/source/ruby.rb', line 5137 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"
5150 5151 5152 5153 5154 5155 5156 5157 5158 |
# File 'lib/source/ruby.rb', line 5150 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 |
#options ⇒ Object
call-seq:
rxp. -> 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/. #=> 0
/cat/i. #=> 1
Regexp.new('cat', true). #=> 1
r = /cat/i
Regexp.new(r.source, r.) #=> /cat/i
5178 5179 5180 5181 5182 5183 5184 |
# File 'lib/source/ruby.rb', line 5178 def `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 |
#source ⇒ Object
call-seq:
rxp.source -> str
Returns the original string of the pattern.
/ab+c/i.source #=> "ab+c"
5193 5194 5195 |
# File 'lib/source/ruby.rb', line 5193 def source `$q(this._source)` end |
#to_s ⇒ Object
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)"
5206 5207 5208 5209 |
# File 'lib/source/ruby.rb', line 5206 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 |