Class: RE2::MatchData

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

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ String? #[](start, length) ⇒ Array<String, nil> #[](range) ⇒ Array<String, nil> #[](name) ⇒ String?

Retrieve zero, one or more matches by index or name.

Overloads:

  • #[](index) ⇒ String?

    Access a particular match by index.

    Examples:

    m = RE2::Regexp.new('(\d+)').match("bob 123")
    m[0]    #=> "123"
    

    Parameters:

    • index (Fixnum)

      the index of the match to fetch

    Returns:

    • (String, nil)

      the specified match

  • #[](start, length) ⇒ Array<String, nil>

    Access a range of matches by starting index and length.

    Examples:

    m = RE2::Regexp.new('(\d+)').match("bob 123")
    m[0, 1]    #=> ["123"]
    

    Parameters:

    • start (Fixnum)

      the index from which to start

    • length (Fixnum)

      the number of elements to fetch

    Returns:

    • (Array<String, nil>)

      the specified matches

  • #[](range) ⇒ Array<String, nil>

    Access a range of matches by index.

    Examples:

    m = RE2::Regexp.new('(\d+)').match("bob 123")
    m[0..1]    #=> "[123", "123"]
    

    Parameters:

    • range (Range)

      the range of match indexes to fetch

    Returns:

    • (Array<String, nil>)

      the specified matches

  • #[](name) ⇒ String?

    Access a particular match by name.

    Examples:

    m = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
    m["number"] #=> "123"
    m[:number]  #=> "123"
    

    Parameters:

    • name (String, Symbol)

      the name of the match to fetch

    Returns:

    • (String, nil)

      the specific match

Returns:



515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'ext/re2/re2.cc', line 515

static VALUE re2_matchdata_aref(int argc, VALUE *argv, VALUE self) {
  VALUE idx, rest;
  rb_scan_args(argc, argv, "11", &idx, &rest);

  if (TYPE(idx) == T_STRING) {
    return re2_matchdata_named_match(StringValuePtr(idx), self);
  } else if (SYMBOL_P(idx)) {
    return re2_matchdata_named_match(rb_id2name(SYM2ID(idx)), self);
  } else if (!NIL_P(rest) || !FIXNUM_P(idx) || FIX2INT(idx) < 0) {
    return rb_ary_aref(argc, argv, re2_matchdata_to_a(self));
  } else {
    return re2_matchdata_nth_match(FIX2INT(idx), self);
  }
}

#beginFixnum

Returns the offset of the start of the nth element of the matchdata.

Examples:

m = RE2::Regexp.new('ob (\d+)').match("bob 123")
m.begin(0)  #=> 1
m.begin(1)  #=> 4

Parameters:

  • n (Fixnum, String, Symbol)

    the name or number of the match

Returns:

  • (Fixnum)

    the offset of the start of the match



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/re2/re2.cc', line 312

static VALUE re2_matchdata_begin(VALUE self, VALUE n) {
  re2_matchdata *m;
  re2_pattern *p;
  re2::StringPiece *match;
  long offset;

  Data_Get_Struct(self, re2_matchdata, m);
  Data_Get_Struct(m->regexp, re2_pattern, p);

  match = re2_matchdata_find_match(n, self);
  if (match == NULL) {
    return Qnil;
  } else {
    offset = reinterpret_cast<uintptr_t>(match->data()) - reinterpret_cast<uintptr_t>(StringValuePtr(m->text));

    return ENCODED_STR_SUBLEN(StringValue(m->text), offset,
           p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
  }
}

#endFixnum

Returns the offset of the character following the end of the nth element of the matchdata.

Examples:

m = RE2::Regexp.new('ob (\d+) b').match("bob 123 bob")
m.end(0)  #=> 9
m.end(1)  #=> 7

Parameters:

  • n (Fixnum, String, Symbol)

    the name or number of the match

Returns:

  • (Fixnum)

    the offset of the character following the end of the match



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/re2/re2.cc', line 342

static VALUE re2_matchdata_end(VALUE self, VALUE n) {
  re2_matchdata *m;
  re2_pattern *p;
  re2::StringPiece *match;
  long offset;

  Data_Get_Struct(self, re2_matchdata, m);
  Data_Get_Struct(m->regexp, re2_pattern, p);

  match = re2_matchdata_find_match(n, self);

  if (match == NULL) {
    return Qnil;
  } else {
    offset = reinterpret_cast<uintptr_t>(match->data()) - reinterpret_cast<uintptr_t>(StringValuePtr(m->text)) + match->size();

    return ENCODED_STR_SUBLEN(StringValue(m->text), offset,
           p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
  }
}

#inspectString

Returns a printable version of the match.

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.inspect    #=> "#<RE2::MatchData \"123\" 1:\"123\">"

Returns:

  • (String)

    a printable version of the match



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'ext/re2/re2.cc', line 547

static VALUE re2_matchdata_inspect(VALUE self) {
  int i;
  re2_matchdata *m;
  re2_pattern *p;
  VALUE match, result;
  ostringstream output;

  Data_Get_Struct(self, re2_matchdata, m);
  Data_Get_Struct(m->regexp, re2_pattern, p);

  output << "#<RE2::MatchData";

  for (i = 0; i < m->number_of_matches; i++) {
    output << " ";

    if (i > 0) {
      output << i << ":";
    }

    match = re2_matchdata_nth_match(i, self);

    if (match == Qnil) {
      output << "nil";
    } else {
      output << "\"" << StringValuePtr(match) << "\"";
    }
  }

  output << ">";

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

  return result;
}

#lengthFixnum

Returns the number of elements in the match array (including nils).

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.size      #=> 2
m.length    #=> 2

Returns:

  • (Fixnum)

    the number of elements



295
296
297
298
299
300
# File 'ext/re2/re2.cc', line 295

static VALUE re2_matchdata_size(VALUE self) {
  re2_matchdata *m;
  Data_Get_Struct(self, re2_matchdata, m);

  return INT2FIX(m->number_of_matches);
}

#regexpRE2::Regexp

Returns the Regexp used in the match.

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.regexp    #=> #<RE2::Regexp /(\d+)/>

Returns:



371
372
373
374
375
# File 'ext/re2/re2.cc', line 371

static VALUE re2_matchdata_regexp(VALUE self) {
  re2_matchdata *m;
  Data_Get_Struct(self, re2_matchdata, m);
  return m->regexp;
}

#sizeFixnum

Returns the number of elements in the match array (including nils).

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.size      #=> 2
m.length    #=> 2

Returns:

  • (Fixnum)

    the number of elements



295
296
297
298
299
300
# File 'ext/re2/re2.cc', line 295

static VALUE re2_matchdata_size(VALUE self) {
  re2_matchdata *m;
  Data_Get_Struct(self, re2_matchdata, m);

  return INT2FIX(m->number_of_matches);
}

#stringString

Returns a frozen copy of the string passed into match.

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.string  #=> "bob 123"

Returns:

  • (String)

    a frozen copy of the passed string.



153
154
155
156
157
158
# File 'ext/re2/re2.cc', line 153

static VALUE re2_matchdata_string(VALUE self) {
  re2_matchdata *m;
  Data_Get_Struct(self, re2_matchdata, m);

  return m->text;
}

#to_aArray<String, nil>

Returns the array of matches.

Examples:

m = RE2::Regexp.new('(\d+)').match("bob 123")
m.to_a    #=> ["123", "123"]

Returns:

  • (Array<String, nil>)

    the array of matches



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'ext/re2/re2.cc', line 405

static VALUE re2_matchdata_to_a(VALUE self) {
  int i;
  re2_matchdata *m;
  re2_pattern *p;
  re2::StringPiece *match;
  VALUE array;

  Data_Get_Struct(self, re2_matchdata, m);
  Data_Get_Struct(m->regexp, re2_pattern, p);

  array = rb_ary_new2(m->number_of_matches);
  for (i = 0; i < m->number_of_matches; i++) {
    match = &m->matches[i];

    if (match->empty()) {
      rb_ary_push(array, Qnil);
    } else {
      rb_ary_push(array, ENCODED_STR_NEW(match->data(), match->size(),
            p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1"));
    }
  }

  return array;
}

#to_sString

Returns the entire matched string.

Returns:

  • (String)

    the entire matched string



535
536
537
# File 'ext/re2/re2.cc', line 535

static VALUE re2_matchdata_to_s(VALUE self) {
  return re2_matchdata_nth_match(0, self);
}