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(str) ⇒ Object
call-seq: Regexp.escape(str) -> string Regexp.quote(str) -> string.
-
.last_match ⇒ Object
FIX: Incomplete.
-
.quote(str) ⇒ Object
call-seq: Regexp.escape(str) -> string Regexp.quote(str) -> string.
-
.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
5081 5082 5083 5084 5085 |
# File 'lib/source/ruby.rb', line 5081 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
5026 5027 5028 |
# File 'lib/source/ruby.rb', line 5026 def self.compile(value,) Regexp.new(value,) 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('\\*?{}.') #=> \\\\\*\?\{\}\.
5040 5041 5042 |
# File 'lib/source/ruby.rb', line 5040 def self.escape(str) `$q(str._value.replace(/([-.*+?^${}()|[\\]\\/\\\\])/g, '\\\\$1'))` end |
.last_match ⇒ Object
FIX: Incomplete
5045 5046 |
# File 'lib/source/ruby.rb', line 5045 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('\\*?{}.') #=> \\\\\*\?\{\}\.
5058 5059 5060 |
# File 'lib/source/ruby.rb', line 5058 def self.quote(str) `str._value.replace(/([-.*+?^${}()|[\\]\\/\\\\])/g, '\\\\$1')` 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
5097 5098 5099 |
# File 'lib/source/ruby.rb', line 5097 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
5117 5118 5119 5120 5121 5122 5123 |
# File 'lib/source/ruby.rb', line 5117 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
5135 5136 5137 5138 5139 5140 5141 |
# File 'lib/source/ruby.rb', line 5135 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.
5152 5153 5154 |
# File 'lib/source/ruby.rb', line 5152 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
5166 5167 5168 |
# File 'lib/source/ruby.rb', line 5166 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
5180 5181 5182 |
# File 'lib/source/ruby.rb', line 5180 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"
5193 5194 5195 5196 5197 5198 5199 5200 5201 |
# File 'lib/source/ruby.rb', line 5193 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
5221 5222 5223 5224 5225 5226 5227 |
# File 'lib/source/ruby.rb', line 5221 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"
5236 5237 5238 |
# File 'lib/source/ruby.rb', line 5236 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)"
5249 5250 5251 5252 |
# File 'lib/source/ruby.rb', line 5249 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 |