Object
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.
ORegexp.escape('\\*?{}.') #=> \\\\\*\?\{\}\.
# File lib/oniguruma.rb, line 100 100: def escape( *args ) 101: Regexp.escape( *args ) 102: end
The first form returns the MatchData object generated by the last successful pattern match. The second form returns the nth field in this MatchData object.
ORegexp.new( 'c(.)t' ) =~ 'cat' #=> 0 ORegexp.last_match #=> #<MatchData:0x401b3d30> ORegexp.last_match(0) #=> "cat" ORegexp.last_match(1) #=> "a" ORegexp.last_match(2) #=> nil
# File lib/oniguruma.rb, line 121 121: def last_match( index = nil) 122: if index 123: @@last_match[index] 124: else 125: @@last_match 126: end 127: end
Constructs a new regular expression from pattern, which is a String. The second parameter may be a Hash of the form:
{ :options => option_value, :encoding => encoding_value, :syntax => syntax_value }
Where option_value is a bitwise OR of Oniguruma::OPTION_XXX constants; encoding_value is one of Oniguruma::ENCODING_XXX constants; and syntax_value is one of Oniguruma::SYNTAX_XXX constants.
r1 = ORegexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/ r2 = ORegexp.new('cat', :options => OPTION_IGNORECASE ) #=> /cat/i r3 = ORegexp.new('dog', :options => OPTION_EXTEND ) #=> /dog/x #Accept java syntax on SJIS encoding: r4 = ORegexp.new('ape', :syntax => SYNTAX_JAVA, :encoding => ENCODING_SJIS) #=> /ape/
Second form uses string shortcuts to set options and encoding:
r = ORegexp.new('cat', 'i', 'utf8', 'java')
# File lib/oniguruma.rb, line 160 160: def initialize( pattern, *args ) 161: defaults = { :options => OPTION_DEFAULT, :encoding => ENCODING_ASCII, :syntax => SYNTAX_DEFAULT} 162: if args[0].is_a?(String) 163: options = {} 164: option_str, encoding_str, syntax_str = *args 165: opt = 0 166: option_str.each_byte {|x| opt |= (OPTIONS_SHORTCUTS[x.chr] || 0) } 167: options[:options] = opt 168: if encoding_str && Oniguruma::const_defined?("ENCODING_#{encoding_str.upcase}") 169: options[:encoding] = Oniguruma::const_get("ENCODING_#{encoding_str.upcase}") 170: end 171: if syntax_str && Oniguruma::const_defined?("SYNTAX_#{syntax_str.upcase}") 172: options[:syntax] = Oniguruma::const_get("SYNTAX_#{syntax_str.upcase}") 173: end 174: else 175: options = args[0] || {} 176: end 177: old_initialize( pattern, defaults.merge( options ).freeze ) 178: end
Equality—Two regexps are equal if their patterns are identical, they have the same character set code, and their # values are the same.
# File lib/oniguruma.rb, line 188 188: def == regexp 189: @pattern == regexp.source && kcode == regexp.kcode && casefold? == regexp.casefold? 190: end
Case Equality—Synonym for ORegexp#=~ used in case statements.
a = "HELLO" case a when ORegexp.new('^[a-z]*$'); print "Lower case\n" when ORegexp.new('^[A-Z]*$'); print "Upper case\n" else; print "Mixed case\n" end
produces:
Upper case
static VALUE oregexp_m_eqq(VALUE self, VALUE str)
Matches rxp against string, returning the offset of the start of the match or nil if the match failed. Sets $~ to the corresponding MatchData or nil.
ORegexp.new( 'SIT' ) =~ "insensitive" #=> nil ORegexp.new( 'SIT', :options => OPTION_IGNORECASE ) =~ "insensitive" #=> 5
static VALUE oregexp_match_op(VALUE self, VALUE str)
Returns the value of the case-insensitive flag.
# File lib/oniguruma.rb, line 198 198: def casefold? 199: (@options[:options] & OPTION_IGNORECASE) > 0 200: end
Returns a copy of str with all occurrences of rxp pattern replaced with either replacement or the value of the block.
If a string is used as the replacement, the sequences 1, 2, and so on may be used to interpolate successive groups in the match.
In the block form, the current MatchData object is passed in as a parameter. The value returned by the block will be substituted for the match on each call.
static VALUE oregexp_m_gsub(int argc, VALUE *argv, VALUE self)
Performs the substitutions of ORegexp#gsub in place, returning str, or nil if no substitutions were performed.
static VALUE oregexp_m_gsub_bang(int argc, VALUE *argv, VALUE self)
Returns a readable version of rxp
ORegexp.new( 'cat', :options => OPTION_MULTILINE | OPTION_IGNORECASE ).inspect => /cat/im ORegexp.new( 'cat', :options => OPTION_MULTILINE | OPTION_IGNORECASE ).to_s => (?im-x)cat
# File lib/oniguruma.rb, line 271 271: def inspect 272: opt_str = "" 273: opt_str += "i" if (@options[:options] & OPTION_IGNORECASE) > 0 274: opt_str += "m" if (@options[:options] & OPTION_MULTILINE) > 0 275: opt_str += "x" if (@options[:options] & OPTION_EXTEND) > 0 276: "/" + @pattern + "/" + opt_str 277: end
Returns the character set code for the regexp.
# File lib/oniguruma.rb, line 206 206: def kcode 207: @options[:encoding] 208: end
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.
ORegexp.new('(.)(.)(.)').match("abc")[2] #=> "b"
The second form allows to perform the match in a region defined by begin and end while still taking into account look-behinds and look-forwards.
ORegexp.new('1*2*').match('11221122').offset => [4,8] ORegexp.new('(?<=2)1*2*').match('11221122').offset => [4,8]
Compare with:
ORegexp.new('(?<=2)1*2*').match('11221122'[4..-1]) => nil
static VALUE oregexp_match( int argc, VALUE * argv, VALUE self )
Returns the set of bits corresponding to the options used when creating this ORegexp (see ORegexp::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 ORegexp::new.
Oniguruma::OPTION_IGNORECASE #=> 1 Oniguruma::OPTION_EXTEND #=> 2 Oniguruma::OPTION_MULTILINE #=> 4 Regexp.new(r.source, :options => Oniguruma::OPTION_EXTEND ) #=> 2
# File lib/oniguruma.rb, line 225 225: def options 226: @options[:options] 227: end
Both forms iterate through str, matching the pattern. For each match, a MatchData object is generated and passed to the block, and added to the resulting array of MatchData objects.
If str does not match pattern, nil is returned.
static VALUE oregexp_m_scan(VALUE self, VALUE str)
Returns the original string of the pattern.
ORegex.new( 'ab+c', 'ix' ).source #=> "ab+c"
# File lib/oniguruma.rb, line 285 285: def source 286: @pattern.freeze 287: end
Returns a copy of str with the first occurrence of rxp pattern replaced with either replacement or the value of the block.
If a string is used as the replacement, the sequences 1, 2, and so on may be used to interpolate successive groups in the match.
In the block form, the current MatchData object is passed in as a parameter. The value returned by the block will be substituted for the match on each call.
static VALUE oregexp_m_sub(int argc, VALUE *argv, VALUE self)
Performs the substitutions of ORegexp#sub in place, returning str, or nil if no substitutions were performed.
static VALUE oregexp_m_sub_bang(int argc, VALUE *argv, VALUE self)
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 = ORegexp.new( 'ab+c', :options OPTION_IGNORECASE | OPTION_EXTEND ) #=> /ab+c/ix s1 = r1.to_s #=> "(?ix-m:ab+c)" r2 = ORegexp.new(s1) #=> /(?ix-m:ab+c)/ r1 == r2 #=> false r1.source #=> "ab+c" r2.source #=> "(?ix-m:ab+c)"
# File lib/oniguruma.rb, line 247 247: def to_s 248: opt_str = "(?" 249: opt_str += "i" if (@options[:options] & OPTION_IGNORECASE) > 0 250: opt_str += "m" if (@options[:options] & OPTION_MULTILINE) > 0 251: opt_str += "x" if (@options[:options] & OPTION_EXTEND) > 0 252: unless opt_str == "(?imx" 253: opt_str += "-" 254: opt_str += "i" if (@options[:options] & OPTION_IGNORECASE) == 0 255: opt_str += "m" if (@options[:options] & OPTION_MULTILINE) == 0 256: opt_str += "x" if (@options[:options] & OPTION_EXTEND) == 0 257: end 258: opt_str += ")" 259: opt_str + @pattern 260: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.