Class | Oniguruma::ORegexp |
In: |
lib/oniguruma.rb
ext/oregexp.c |
Parent: | Object |
call-seq:
rxp =~ string => int or nil
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
scan | -> | match_all |
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('\\*?{}.') #=> \\\\\*\?\{\}\.
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
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')
Equality—Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same.
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
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.
Performs the substitutions of ORegexp#gsub in place, returning str, or nil if no substitutions were performed.
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
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
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
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.
Performs the substitutions of ORegexp#sub in place, returning str, or nil if no substitutions were performed.
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)"