Class: RE2::Regexp

Inherits:
Object
  • Object
show all
Defined in:
ext/re2/re2.cc

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ RE2::Regexp #initialize(pattern, options) ⇒ RE2::Regexp

Returns a new RE2::Regexp object with a compiled version of pattern stored inside.

Overloads:

  • #initialize(pattern) ⇒ RE2::Regexp

    Returns a new RE2::Regexp object with a compiled version of pattern stored inside with the default options.

    Raises:

    • (NoMemoryError)

      if memory could not be allocated for the compiled pattern

  • #initialize(pattern, options) ⇒ RE2::Regexp

    Returns a new RE2::Regexp object with a compiled version of pattern stored inside with the specified options.

    Options Hash (options):

    • :utf8 (Boolean) — default: true

      text and pattern are UTF-8; otherwise Latin-1

    • :posix_syntax (Boolean) — default: false

      restrict regexps to POSIX egrep syntax

    • :longest_match (Boolean) — default: false

      search for longest match, not first match

    • :log_errors (Boolean) — default: true

      log syntax and execution errors to ERROR

    • :max_mem (Fixnum)

      approx. max memory footprint of RE2

    • :literal (Boolean) — default: false

      interpret string as literal, not regexp

    • :never_nl (Boolean) — default: false

      never match n, even if it is in regexp

    • :case_sensitive (Boolean) — default: true

      match is case-sensitive (regexp can override with (?i) unless in posix_syntax mode)

    • :perl_classes (Boolean) — default: false

      allow Perl's d s w D S W when in posix_syntax mode

    • :word_boundary (Boolean) — default: false

      allow b B (word boundary and not) when in posix_syntax mode

    • :one_line (Boolean) — default: false

      ^ and $ only match beginning and end of text when in posix_syntax mode

    Raises:

    • (NoMemoryError)

      if memory could not be allocated for the compiled pattern



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'ext/re2/re2.cc', line 633

static VALUE re2_regexp_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE pattern, options, utf8, posix_syntax, longest_match, log_errors,
        max_mem, literal, never_nl, case_sensitive, perl_classes,
        word_boundary, one_line;
  re2_pattern *p;

  rb_scan_args(argc, argv, "11", &pattern, &options);
  Data_Get_Struct(self, re2_pattern, p);

  if (RTEST(options)) {
    if (TYPE(options) != T_HASH) {
      rb_raise(rb_eArgError, "options should be a hash");
    }

    RE2::Options re2_options;

    utf8 = rb_hash_aref(options, ID2SYM(id_utf8));
    if (!NIL_P(utf8)) {
      re2_options.set_utf8(RTEST(utf8));
    }

    posix_syntax = rb_hash_aref(options, ID2SYM(id_posix_syntax));
    if (!NIL_P(posix_syntax)) {
      re2_options.set_posix_syntax(RTEST(posix_syntax));
    }

    longest_match = rb_hash_aref(options, ID2SYM(id_longest_match));
    if (!NIL_P(longest_match)) {
      re2_options.set_longest_match(RTEST(longest_match));
    }

    log_errors = rb_hash_aref(options, ID2SYM(id_log_errors));
    if (!NIL_P(log_errors)) {
      re2_options.set_log_errors(RTEST(log_errors));
    }

    max_mem = rb_hash_aref(options, ID2SYM(id_max_mem));
    if (!NIL_P(max_mem)) {
      re2_options.set_max_mem(NUM2INT(max_mem));
    }

    literal = rb_hash_aref(options, ID2SYM(id_literal));
    if (!NIL_P(literal)) {
      re2_options.set_literal(RTEST(literal));
    }

    never_nl = rb_hash_aref(options, ID2SYM(id_never_nl));
    if (!NIL_P(never_nl)) {
      re2_options.set_never_nl(RTEST(never_nl));
    }

    case_sensitive = rb_hash_aref(options, ID2SYM(id_case_sensitive));
    if (!NIL_P(case_sensitive)) {
      re2_options.set_case_sensitive(RTEST(case_sensitive));
    }

    perl_classes = rb_hash_aref(options, ID2SYM(id_perl_classes));
    if (!NIL_P(perl_classes)) {
      re2_options.set_perl_classes(RTEST(perl_classes));
    }

    word_boundary = rb_hash_aref(options, ID2SYM(id_word_boundary));
    if (!NIL_P(word_boundary)) {
      re2_options.set_word_boundary(RTEST(word_boundary));
    }

    one_line = rb_hash_aref(options, ID2SYM(id_one_line));
    if (!NIL_P(one_line)) {
      re2_options.set_one_line(RTEST(one_line));
    }

    p->pattern = new(nothrow) RE2(StringValuePtr(pattern), re2_options);
  } else {
    p->pattern = new(nothrow) RE2(StringValuePtr(pattern));
  }

  if (p->pattern == 0) {
    rb_raise(rb_eNoMemError, "not enough memory to allocate RE2 object");
  }

  return self;
}

Class Method Details

.compileObject

.escapeString

Returns a version of str with all potentially meaningful regexp characters escaped. The returned string, used as a regular expression, will exactly match the original string.

Examples:

RE2::Regexp.escape("1.5-2.0?")    #=> "1\.5\-2\.0\?"


1307
1308
1309
1310
1311
# File 'ext/re2/re2.cc', line 1307

static VALUE re2_QuoteMeta(VALUE self, VALUE unquoted) {
  UNUSED(self);
  string quoted_string = RE2::QuoteMeta(StringValuePtr(unquoted));
  return rb_str_new(quoted_string.data(), quoted_string.size());
}

.quoteString

Returns a version of str with all potentially meaningful regexp characters escaped. The returned string, used as a regular expression, will exactly match the original string.

Examples:

RE2::Regexp.escape("1.5-2.0?")    #=> "1\.5\-2\.0\?"


1307
1308
1309
1310
1311
# File 'ext/re2/re2.cc', line 1307

static VALUE re2_QuoteMeta(VALUE self, VALUE unquoted) {
  UNUSED(self);
  string quoted_string = RE2::QuoteMeta(StringValuePtr(unquoted));
  return rb_str_new(quoted_string.data(), quoted_string.size());
}

Instance Method Details

#===Boolean

Returns true or false to indicate a successful match. Equivalent to re2.match(text, 0).



1192
1193
1194
1195
1196
1197
1198
# File 'ext/re2/re2.cc', line 1192

static VALUE re2_regexp_match_query(VALUE self, VALUE text) {
  VALUE argv[2];
  argv[0] = text;
  argv[1] = INT2FIX(0);

  return re2_regexp_match(2, argv, self);
}

#=~Boolean

Returns true or false to indicate a successful match. Equivalent to re2.match(text, 0).



1192
1193
1194
1195
1196
1197
1198
# File 'ext/re2/re2.cc', line 1192

static VALUE re2_regexp_match_query(VALUE self, VALUE text) {
  VALUE argv[2];
  argv[0] = text;
  argv[1] = INT2FIX(0);

  return re2_regexp_match(2, argv, self);
}

#case_insensitive?Boolean

Returns whether or not the regular expression re2 was compiled with the case_sensitive option set to false.

Examples:

re2 = RE2::Regexp.new("woo?", :case_sensitive => true)
re2.case_insensitive?    #=> false
re2.casefold?    #=> false


900
901
902
# File 'ext/re2/re2.cc', line 900

static VALUE re2_regexp_case_insensitive(VALUE self) {
  return BOOL2RUBY(re2_regexp_case_sensitive(self) != Qtrue);
}

#case_sensitive?Boolean

Returns whether or not the regular expression re2 was compiled with the case_sensitive option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :case_sensitive => true)
re2.case_sensitive?    #=> true


884
885
886
887
888
# File 'ext/re2/re2.cc', line 884

static VALUE re2_regexp_case_sensitive(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().case_sensitive());
}

#casefold?Boolean

Returns whether or not the regular expression re2 was compiled with the case_sensitive option set to false.

Examples:

re2 = RE2::Regexp.new("woo?", :case_sensitive => true)
re2.case_insensitive?    #=> false
re2.casefold?    #=> false


900
901
902
# File 'ext/re2/re2.cc', line 900

static VALUE re2_regexp_case_insensitive(VALUE self) {
  return BOOL2RUBY(re2_regexp_case_sensitive(self) != Qtrue);
}

#errorString?

If the RE2 could not be created properly, returns an error string otherwise returns nil.



955
956
957
958
959
960
961
962
963
# File 'ext/re2/re2.cc', line 955

static VALUE re2_regexp_error(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  if (p->pattern->ok()) {
    return Qnil;
  } else {
    return rb_str_new(p->pattern->error().data(), p->pattern->error().size());
  }
}

#error_argString?

If the RE2 could not be created properly, returns the offending portion of the regexp otherwise returns nil.



971
972
973
974
975
976
977
978
979
980
981
# File 'ext/re2/re2.cc', line 971

static VALUE re2_regexp_error_arg(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  if (p->pattern->ok()) {
    return Qnil;
  } else {
    return ENCODED_STR_NEW(p->pattern->error_arg().data(),
        p->pattern->error_arg().size(),
        p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
  }
}

#inspectString

Returns a printable version of the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.inspect    #=> "#<RE2::Regexp /woo?/>"


724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'ext/re2/re2.cc', line 724

static VALUE re2_regexp_inspect(VALUE self) {
  re2_pattern *p;
  VALUE result;
  ostringstream output;

  Data_Get_Struct(self, re2_pattern, p);

  output << "#<RE2::Regexp /" << p->pattern->pattern() << "/>";

  result = ENCODED_STR_NEW(output.str().data(), output.str().length(),
      p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");

  return result;
}

#literal?Boolean

Returns whether or not the regular expression re2 was compiled with the literal option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :literal => true)
re2.literal?    #=> true


854
855
856
857
858
# File 'ext/re2/re2.cc', line 854

static VALUE re2_regexp_literal(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().literal());
}

#log_errors?Boolean

Returns whether or not the regular expression re2 was compiled with the log_errors option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :log_errors => true)
re2.log_errors?    #=> true


824
825
826
827
828
# File 'ext/re2/re2.cc', line 824

static VALUE re2_regexp_log_errors(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().log_errors());
}

#longest_match?Boolean

Returns whether or not the regular expression re2 was compiled with the longest_match option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :longest_match => true)
re2.longest_match?    #=> true


809
810
811
812
813
# File 'ext/re2/re2.cc', line 809

static VALUE re2_regexp_longest_match(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().longest_match());
}

#match(text) ⇒ RE2::MatchData #match(text, 0) ⇒ Boolean #match(text, number_of_matches) ⇒ RE2::MatchData

Match the pattern against the given text and return either a boolean (if no submatches are required) or a MatchData instance.

Overloads:

  • #match(text) ⇒ RE2::MatchData

    Returns an MatchData containing the matching pattern and all subpatterns resulting from looking for the regexp in text.

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    r.match('woo')    #=> #<RE2::MatchData "woo" 1:"o" 2:"o">

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches

  • #match(text, 0) ⇒ Boolean

    Returns either true or false indicating whether a successful match was made.

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    r.match('woo', 0) #=> true
    r.match('bob', 0) #=> false

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches

  • #match(text, number_of_matches) ⇒ RE2::MatchData

    See match(text) but with a specific number of matches returned (padded with nils if necessary).

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    r.match('woo', 1) #=> #<RE2::MatchData "woo" 1:"o">
    r.match('woo', 3) #=> #<RE2::MatchData "woo" 1:"o" 2:"o" 3:nil>

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
# File 'ext/re2/re2.cc', line 1131

static VALUE re2_regexp_match(int argc, VALUE *argv, VALUE self) {
  int n;
  bool matched;
  re2_pattern *p;
  re2_matchdata *m;
  VALUE text, number_of_matches, matchdata;

  rb_scan_args(argc, argv, "11", &text, &number_of_matches);

  /* Ensure text is a string. */
  text = StringValue(text);

  Data_Get_Struct(self, re2_pattern, p);

  if (RTEST(number_of_matches)) {
    n = NUM2INT(number_of_matches);
  } else {
    n = p->pattern->NumberOfCapturingGroups();
  }

  if (n == 0) {
    matched = match(p->pattern, StringValuePtr(text), 0,
        static_cast<int>(RSTRING_LEN(text)), RE2::UNANCHORED, 0, 0);
    return BOOL2RUBY(matched);
  } else {

    /* Because match returns the whole match as well. */
    n += 1;

    matchdata = rb_class_new_instance(0, 0, re2_cMatchData);
    Data_Get_Struct(matchdata, re2_matchdata, m);
    m->matches = new(nothrow) re2::StringPiece[n];
    m->regexp = self;
    m->text = rb_str_dup(text);
    rb_str_freeze(m->text);

    if (m->matches == 0) {
      rb_raise(rb_eNoMemError,
               "not enough memory to allocate StringPieces for matches");
    }

    m->number_of_matches = n;

    matched = match(p->pattern, StringValuePtr(m->text), 0,
                    static_cast<int>(RSTRING_LEN(m->text)),
                    RE2::UNANCHORED, m->matches, n);

    if (matched) {
      return matchdata;
    } else {
      return Qnil;
    }
  }
}

#match?Boolean

Returns true or false to indicate a successful match. Equivalent to re2.match(text, 0).



1192
1193
1194
1195
1196
1197
1198
# File 'ext/re2/re2.cc', line 1192

static VALUE re2_regexp_match_query(VALUE self, VALUE text) {
  VALUE argv[2];
  argv[0] = text;
  argv[1] = INT2FIX(0);

  return re2_regexp_match(2, argv, self);
}

#max_memFixnum

Returns the max_mem setting for the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?", :max_mem => 1024)
re2.max_mem    #=> 1024


839
840
841
842
843
# File 'ext/re2/re2.cc', line 839

static VALUE re2_regexp_max_mem(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return INT2FIX(p->pattern->options().max_mem());
}

#named_capturing_groupsHash

Returns a hash of names to capturing indices of groups.



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'ext/re2/re2.cc', line 1067

static VALUE re2_regexp_named_capturing_groups(VALUE self) {
  VALUE capturing_groups;
  re2_pattern *p;
  map<string, int> groups;
  map<string, int>::iterator iterator;

  Data_Get_Struct(self, re2_pattern, p);
  groups = p->pattern->NamedCapturingGroups();
  capturing_groups = rb_hash_new();

  for (iterator = groups.begin(); iterator != groups.end(); iterator++) {
    rb_hash_aset(capturing_groups,
        ENCODED_STR_NEW(iterator->first.data(), iterator->first.size(),
          p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1"),
        INT2FIX(iterator->second));
  }

  return capturing_groups;
}

#never_nl?Boolean

Returns whether or not the regular expression re2 was compiled with the never_nl option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :never_nl => true)
re2.never_nl?    #=> true


869
870
871
872
873
# File 'ext/re2/re2.cc', line 869

static VALUE re2_regexp_never_nl(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().never_nl());
}

#number_of_capturing_groupsFixnum

Returns the number of capturing subpatterns, or -1 if the regexp wasn't valid on construction. The overall match ($0) does not count: if the regexp is “(a)(b)”, returns 2.



1055
1056
1057
1058
1059
1060
# File 'ext/re2/re2.cc', line 1055

static VALUE re2_regexp_number_of_capturing_groups(VALUE self) {
  re2_pattern *p;

  Data_Get_Struct(self, re2_pattern, p);
  return INT2FIX(p->pattern->NumberOfCapturingGroups());
}

#ok?Boolean

Returns whether or not the regular expression re2 was compiled successfully or not.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.ok?    #=> true


764
765
766
767
768
# File 'ext/re2/re2.cc', line 764

static VALUE re2_regexp_ok(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->ok());
}

#one_line?Boolean

Returns whether or not the regular expression re2 was compiled with the one_line option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :one_line => true)
re2.one_line?    #=> true


943
944
945
946
947
# File 'ext/re2/re2.cc', line 943

static VALUE re2_regexp_one_line(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().one_line());
}

#optionsHash

Returns a hash of the options currently set for re2.



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'ext/re2/re2.cc', line 1002

static VALUE re2_regexp_options(VALUE self) {
  VALUE options;
  re2_pattern *p;

  Data_Get_Struct(self, re2_pattern, p);
  options = rb_hash_new();

  rb_hash_aset(options, ID2SYM(id_utf8),
      BOOL2RUBY(p->pattern->options().utf8()));

  rb_hash_aset(options, ID2SYM(id_posix_syntax),
      BOOL2RUBY(p->pattern->options().posix_syntax()));

  rb_hash_aset(options, ID2SYM(id_longest_match),
      BOOL2RUBY(p->pattern->options().longest_match()));

  rb_hash_aset(options, ID2SYM(id_log_errors),
      BOOL2RUBY(p->pattern->options().log_errors()));

  rb_hash_aset(options, ID2SYM(id_max_mem),
      INT2FIX(p->pattern->options().max_mem()));

  rb_hash_aset(options, ID2SYM(id_literal),
      BOOL2RUBY(p->pattern->options().literal()));

  rb_hash_aset(options, ID2SYM(id_never_nl),
      BOOL2RUBY(p->pattern->options().never_nl()));

  rb_hash_aset(options, ID2SYM(id_case_sensitive),
      BOOL2RUBY(p->pattern->options().case_sensitive()));

  rb_hash_aset(options, ID2SYM(id_perl_classes),
      BOOL2RUBY(p->pattern->options().perl_classes()));

  rb_hash_aset(options, ID2SYM(id_word_boundary),
      BOOL2RUBY(p->pattern->options().word_boundary()));

  rb_hash_aset(options, ID2SYM(id_one_line),
      BOOL2RUBY(p->pattern->options().one_line()));

  /* This is a read-only hash after all... */
  rb_obj_freeze(options);

  return options;
}

#patternString

Returns a string version of the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.to_s    #=> "woo?"


747
748
749
750
751
752
753
# File 'ext/re2/re2.cc', line 747

static VALUE re2_regexp_to_s(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return ENCODED_STR_NEW(p->pattern->pattern().data(),
      p->pattern->pattern().size(),
      p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
}

#perl_classes?Boolean

Returns whether or not the regular expression re2 was compiled with the perl_classes option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :perl_classes => true)
re2.perl_classes?    #=> true


913
914
915
916
917
# File 'ext/re2/re2.cc', line 913

static VALUE re2_regexp_perl_classes(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().perl_classes());
}

#posix_syntax?Boolean

Returns whether or not the regular expression re2 was compiled with the posix_syntax option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :posix_syntax => true)
re2.posix_syntax?    #=> true


794
795
796
797
798
# File 'ext/re2/re2.cc', line 794

static VALUE re2_regexp_posix_syntax(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().posix_syntax());
}

#program_sizeFixnum

Returns the program size, a very approximate measure of a regexp's “cost”. Larger numbers are more expensive than smaller numbers.



990
991
992
993
994
# File 'ext/re2/re2.cc', line 990

static VALUE re2_regexp_program_size(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return INT2FIX(p->pattern->ProgramSize());
}

#scanObject

Returns a Scanner for scanning the given text incrementally.

Examples:

c = RE2::Regexp.new('(\w+)').scan("Foo bar baz")


1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'ext/re2/re2.cc', line 1206

static VALUE re2_regexp_scan(VALUE self, VALUE text) {
  re2_pattern *p;
  re2_scanner *c;
  VALUE scanner;

  Data_Get_Struct(self, re2_pattern, p);
  scanner = rb_class_new_instance(0, 0, re2_cScanner);
  Data_Get_Struct(scanner, re2_scanner, c);

  c->input = new(nothrow) re2::StringPiece(StringValuePtr(text));
  c->regexp = self;
  c->text = text;
  c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups();

  return scanner;
}

#sourceString

Returns a string version of the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.to_s    #=> "woo?"


747
748
749
750
751
752
753
# File 'ext/re2/re2.cc', line 747

static VALUE re2_regexp_to_s(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return ENCODED_STR_NEW(p->pattern->pattern().data(),
      p->pattern->pattern().size(),
      p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
}

#to_sString

Returns a string version of the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.to_s    #=> "woo?"


747
748
749
750
751
752
753
# File 'ext/re2/re2.cc', line 747

static VALUE re2_regexp_to_s(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return ENCODED_STR_NEW(p->pattern->pattern().data(),
      p->pattern->pattern().size(),
      p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
}

#to_strString

Returns a string version of the regular expression re2.

Examples:

re2 = RE2::Regexp.new("woo?")
re2.to_s    #=> "woo?"


747
748
749
750
751
752
753
# File 'ext/re2/re2.cc', line 747

static VALUE re2_regexp_to_s(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return ENCODED_STR_NEW(p->pattern->pattern().data(),
      p->pattern->pattern().size(),
      p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
}

#utf8?Boolean

Returns whether or not the regular expression re2 was compiled with the utf8 option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :utf8 => true)
re2.utf8?    #=> true


779
780
781
782
783
# File 'ext/re2/re2.cc', line 779

static VALUE re2_regexp_utf8(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().utf8());
}

#word_boundary?Boolean

Returns whether or not the regular expression re2 was compiled with the word_boundary option set to true.

Examples:

re2 = RE2::Regexp.new("woo?", :word_boundary => true)
re2.word_boundary?    #=> true


928
929
930
931
932
# File 'ext/re2/re2.cc', line 928

static VALUE re2_regexp_word_boundary(VALUE self) {
  re2_pattern *p;
  Data_Get_Struct(self, re2_pattern, p);
  return BOOL2RUBY(p->pattern->options().word_boundary());
}