Class | Oniguruma::ORegexp |
In: |
lib/oniguruma.rb
|
Parent: | Object |
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.
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 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 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)"