Module: RE2

Defined in:
lib/re2/string.rb,
lib/re2/scanner.rb,
ext/re2/re2.cc

Defined Under Namespace

Modules: String Classes: MatchData, Regexp, Scanner

Class Method Summary collapse

Class Method Details

.GlobalReplace(str, pattern, rewrite) ⇒ String

Return a copy of str with pattern replaced by rewrite.

Examples:

re2 = RE2.new("oo?")
RE2.GlobalReplace("whoops-doops", re2, "e")  #=> "wheps-deps"
RE2.GlobalReplace("hello there", "e", "i")   #=> "hillo thiri"

Parameters:

  • str (String)

    the string to modify

  • pattern (String, RE2::Regexp)

    a regexp matching text to be replaced

  • rewrite (String)

    the string to replace with

Returns:

  • (String)

    the resulting string



1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'ext/re2/re2.cc', line 1310

static VALUE re2_GlobalReplace(VALUE self, VALUE str, VALUE pattern,
                               VALUE rewrite) {
  UNUSED(self);

  /* Convert all the inputs to be pumped into RE2::GlobalReplace. */
  re2_pattern *p;
  string str_as_string(StringValuePtr(str));

  /* Do the replacement. */
  if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
    Data_Get_Struct(pattern, re2_pattern, p);
    RE2::GlobalReplace(&str_as_string, *p->pattern, StringValuePtr(rewrite));

    return ENCODED_STR_NEW(str_as_string.data(), str_as_string.size(),
        p->pattern->options().encoding() == RE2::Options::EncodingUTF8 ? "UTF-8" : "ISO-8859-1");
  } else {
    RE2::GlobalReplace(&str_as_string, StringValuePtr(pattern),
                       StringValuePtr(rewrite));

    return ENCODED_STR_NEW2(str_as_string.data(), str_as_string.size(),
        pattern);
  }
}

.QuoteMeta(unquoted) ⇒ String

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\?"

Parameters:

  • unquoted (String)

    the unquoted string

Returns:

  • (String)

    the escaped string



1344
1345
1346
1347
1348
# File 'ext/re2/re2.cc', line 1344

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());
}

.Replace(str, pattern, rewrite) ⇒ String

Returns a copy of str with the first occurrence pattern replaced with rewrite.

Examples:

RE2.Replace("hello there", "hello", "howdy") #=> "howdy there"
re2 = RE2.new("hel+o")
RE2.Replace("hello there", re2, "yo")        #=> "yo there"

Parameters:

  • str (String)

    the string to modify

  • pattern (String, RE2::Regexp)

    a regexp matching text to be replaced

  • rewrite (String)

    the string to replace with

Returns:

  • (String)

    the resulting string



1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'ext/re2/re2.cc', line 1273

static VALUE re2_Replace(VALUE self, VALUE str, VALUE pattern,
    VALUE rewrite) {
  UNUSED(self);
  re2_pattern *p;

  /* Convert all the inputs to be pumped into RE2::Replace. */
  string str_as_string(StringValuePtr(str));

  /* Do the replacement. */
  if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
    Data_Get_Struct(pattern, re2_pattern, p);
    RE2::Replace(&str_as_string, *p->pattern, StringValuePtr(rewrite));

    return ENCODED_STR_NEW(str_as_string.data(), str_as_string.size(),
        p->pattern->options().encoding() == RE2::Options::EncodingUTF8 ? "UTF-8" : "ISO-8859-1");
  } else {
    RE2::Replace(&str_as_string, StringValuePtr(pattern),
        StringValuePtr(rewrite));

    return ENCODED_STR_NEW2(str_as_string.data(), str_as_string.size(),
        pattern);
  }

}