Module: RE2

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

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


1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
# File 'ext/re2/re2.cc', line 1325

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


1359
1360
1361
1362
1363
# File 'ext/re2/re2.cc', line 1359

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


1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
# File 'ext/re2/re2.cc', line 1288

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

}