# Regexp#names, MatchData#names, MatchData#regexp

**URL:** <https://www.ruby-forum.com/t/regexp-names-matchdata-names-matchdata-regexp/122551>\
**Category:** Development\
**Created:** [December 9, 2007, 9:20am UTC](https://www.ruby-forum.com/t/regexp-names-matchdata-names-matchdata-regexp/122551 "2007-12-09T09:20:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [December 9, 2007, 9:20am UTC](https://www.ruby-forum.com/t/regexp-names-matchdata-names-matchdata-regexp/122551/1 "2007-12-09T09:20:59Z")

</div>

named capture e$B$^$o$j$r$=$m$($F$$$F!"$d$O$jL\>A0$N%j%9%H$rF@$ke(B  
e$B%a%=%C%I$,I,MW$@$m$&$H$$$&$3$H$G!“e(BRegexp#names e$B$He(B  
MatchData#names e$B$r\<BAu$7$F$\_$^$7$?$,!”$I$&$G$7$g$&$+!#e(B

e$B$"$H!“e(Bmatch.names e$B$Oe(B match.regexp.names  
e$B$HF1$8!”$H%I%-%e%a%se(B  
e$B%H$K=q$3$&$H$7$?$N$G$9$,e(B MatchData#regexp e$B$,$J$+$C$?$N$G$3$le(B  
e$B$b$D$1$^$7$?!#e(B

# Index: re.c

— re.c (revision 14159)  
+++ re.c (working copy)  
@@ -513,6 +513,38 @@ rb\_reg\_options\_m(VALUE re)  
return INT2NUM(options);  
}

+static int  
+reg\_names\_iter(const OnigUChar \*name, const OnigUChar \*name\_end,

- 

```
     int back_num, int *back_refs, OnigRegex regex, void *arg)

```

+{

- VALUE ary = (VALUE)arg;
- rb\_ary\_push(ary, rb\_str\_new((const char \*)name, name\_end-name));
- return 0;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - rxp.names =\> [name1, name2, …]

- 
  - 

- 
  - Returns an array of names of named captures.

- 
  - 

- 
  - 

```
/(?<foo>.)(?<bar>.)(?<baz>.)/.names

```

- 
  - 

```
#=> ["foo", "bar", "baz"]

```

- 
  - 

- 
  - 

```
/(?<foo>.)(?<foo>.)/.names

```

- 
  - 

```
#=> ["foo"]

```

- 
  - 

- 
  - 

```
/(.)(.)/.names'

```

- 
  - 

```
#=> []

```

- \*/
- 

+static VALUE  
+rb\_reg\_names(VALUE re)  
+{

- VALUE ary = rb\_ary\_new();
- onig\_foreach\_name(RREGEXP(re)-\>ptr, reg\_names\_iter, (void\*)ary);
- return ary;  
+}

static Regexp\*  
make\_regexp(const char \*s, long len, rb\_encoding \*enc, int flags,  
onig\_errmsg\_buffer err)  
@@ -595,6 +627,42 @@ match\_init\_copy(VALUE obj, VALUE orig)

/\*

- 
  - call-seq:

- 
  - mtch.regexp =\> regexp

- 
  - 

- 
  - Returns the regexp.

- 
  - 

- 
  - 

```
m = /a.*b/.match("abc")

```

- 
  - 

```
m.regexp #=> /a.*b/

```

- \*/
- 

+static VALUE  
+match\_regexp(VALUE match)  
+{

- return RMATCH(match)-\>regexp;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - mtch.names =\> [name1, name2, …]

- 
  - 

- 
  - Returns an array of names of named captures.

- 
  - It is same as mtch.regexp.names.

- 
  - 

- 
  - 

```
/(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names

```

- 
  - 

```
#=> ["foo", "bar", "baz"]

```

- 
  - 

- 
  - 

```
m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" 

```

y:nil\>

- 
  - 

```
m.names #=> ["x", "y"]

```

- \*/
- 

+static VALUE  
+match\_names(VALUE match)  
+{

- return rb\_reg\_names(RMATCH(match)-\>regexp);  
+}
- 

+/\*

- call-seq:
- 

```
mtch.length => integer

```

- 

```
mtch.size => integer

```

@@ -2754,6 +2822,7 @@ Init\_Regexp(void)  
rb\_define\_method(rb\_cRegexp, “options”, rb\_reg\_options\_m, 0);  
rb\_define\_method(rb\_cRegexp, “encoding”, rb\_obj\_encoding, 0); /\* in  
encoding.c \*/  
rb\_define\_method(rb\_cRegexp, “fixed\_encoding?”,  
rb\_reg\_fixed\_encoding\_p, 0);

- rb\_define\_method(rb\_cRegexp, “names”, rb\_reg\_names, 0);

- rb\_define\_method(rb\_cMatch, “regexp”, match\_regexp, 0);

- rb\_define\_method(rb\_cMatch, “names”, match\_names, 0);  
rb\_define\_method(rb\_cMatch, “size”, match\_size, 0);  
rb\_define\_method(rb\_cMatch, “length”, match\_size, 0);  
rb\_define\_method(rb\_cMatch, “offset”, match\_offset, 1);

---

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [December 9, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/regexp-names-matchdata-names-matchdata-regexp/122551/2 "2007-12-09T16:10:20Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Tanaka A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> named capture e$B$^$o$j$r$=$m$($F$$$F!"$d$O$jL\>A0$N%j%9%H$rF@$ke(B  
> e$B%a%=%C%I$,I,MW$@$m$&$H$$$&$3$H$G!“e(BRegexp#names e$B$He(B  
> MatchData#names e$B$r\<BAu$7$F$\_$^$7$?$,!”$I$&$G$7$g$&$+!#e(B
> 
> e$B$"$H!“e(Bmatch.names e$B$Oe(B match.regexp.names e$B$HF1$8!”$H%I%-%e%a%se(B  
> e$B%H$K=q$3$&$H$7$?$N$G$9$,e(B MatchData#regexp e$B$,$J$+$C$?$N$G$3$le(B  
> e$B$b$D$1$^$7$?!#e(B

e$B$&$%$`!#e(BMatchData#pretty\_print e$B$GL\>A0$b=P$=$&$H;W$C$?$ie(B  
names e$B$@$1$G$O\>pJs$,B-$j$J$+$C$?$N$G!“e(BRegexp#named\_captures  
e$B$GL\>A0$+$i%$%s%G%C%/%9$N%j%9%H$X$N%O%C%7%e$rJV$9$h$&$K$7$F$\_e(B  
e$B$^$7$?!#$3$N7A$O54\<V$,e(B onig\_foreach\_name e$B$GDs6!$7$F$/$l$k$be(B  
e$B$N$H4pK\E\*$KF1$8$G$9!#e(B  
(e$B$J$\*!”$3$3$G$be(B MatchData#regexp e$B$,I,MW$K$J$j$^$7$?!#e(B)

e$B$I$&$G$7$g$&$+!#e(B

# Index: re.c

— re.c (revision 14161)  
+++ re.c (working copy)  
@@ -523,6 +523,84 @@ rb\_reg\_options\_m(VALUE re)  
return INT2NUM(options);  
}

+static int  
+reg\_names\_iter(const OnigUChar \*name, const OnigUChar \*name\_end,

- 

```
     int back_num, int *back_refs, OnigRegex regex, void *arg)

```

+{

- VALUE ary = (VALUE)arg;
- rb\_ary\_push(ary, rb\_str\_new((const char \*)name, name\_end-name));
- return 0;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - rxp.names =\> [name1, name2, …]

- 
  - 

- 
  - Returns a list of names of captures as an array of strings.

- 
  - 

- 
  - 

```
/(?<foo>.)(?<bar>.)(?<baz>.)/.names

```

- 
  - 

```
#=> ["foo", "bar", "baz"]

```

- 
  - 

- 
  - 

```
/(?<foo>.)(?<foo>.)/.names

```

- 
  - 

```
#=> ["foo"]

```

- 
  - 

- 
  - 

```
/(.)(.)/.names'

```

- 
  - 

```
#=> []

```

- \*/
- 

+static VALUE  
+rb\_reg\_names(VALUE re)  
+{

- VALUE ary = rb\_ary\_new();
- onig\_foreach\_name(RREGEXP(re)-\>ptr, reg\_names\_iter, (void\*)ary);
- return ary;  
+}
- 

+static int  
+reg\_named\_captures\_iter(const OnigUChar \*name, const OnigUChar  
\*name\_end,

- 

```
     int back_num, int *back_refs, OnigRegex regex, void *arg)

```

+{

- VALUE hash = (VALUE)arg;
- VALUE ary = rb\_ary\_new2(back\_num);
- int i;
- 
- for(i = 0; i \< back\_num; i++)
- 

```
   rb_ary_store(ary, i, INT2NUM(back_refs[i]));

```

- 
- rb\_hash\_aset(hash, rb\_str\_new((const char\*)name,  
name\_end-name),ary);
- 
- return 0;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - rxp.named\_captures =\> hash

- 
  - 

- 
  - Returns a hash representing information about named captures of  
_rxp_.

- 
  - 

- 
  - A key of the hash is a name of the named captures.

- 
  - A value of the hash is an array which is list of indexes of  
corresponding

- 
  - named captures.

- 
  - 

- 
  - /(?.)(?.)/.named\_captures

- 
  - #=\> {“foo”=\>[1], “bar”=\>[2]}

- 
  - 

- 
  - /(?.)(?.)/.named\_captures’

- 
  - #=\> {“foo”=\>[1, 2]}

- 
  - 

- 
  - If there are no named captures, an empty hash is returned.

- 
  - 

- 
  - /(.)(.)/.named\_captures’

- 
  - #=\> {}

- \*/
- 

+static VALUE  
+rb\_reg\_named\_captures(VALUE re)  
+{

- VALUE hash = rb\_hash\_new();
- onig\_foreach\_name(RREGEXP(re)-\>ptr, reg\_named\_captures\_iter,  
(void\*)hash);
- return hash;  
+}

static Regexp\*  
make\_regexp(const char \*s, long len, rb\_encoding \*enc, int flags,  
onig\_errmsg\_buffer err)  
@@ -604,6 +682,42 @@ match\_init\_copy(VALUE obj, VALUE orig)

/\*

- 
  - call-seq:

- 
  - mtch.regexp =\> regexp

- 
  - 

- 
  - Returns the regexp.

- 
  - 

- 
  - 

```
m = /a.*b/.match("abc")

```

- 
  - 

```
m.regexp #=> /a.*b/

```

- \*/
- 

+static VALUE  
+match\_regexp(VALUE match)  
+{

- return RMATCH(match)-\>regexp;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - mtch.names =\> [name1, name2, …]

- 
  - 

- 
  - Returns a list of names of captures as an array of strings.

- 
  - It is same as mtch.regexp.names.

- 
  - 

- 
  - 

```
/(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names

```

- 
  - 

```
#=> ["foo", "bar", "baz"]

```

- 
  - 

- 
  - 

```
m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" 

```

y:nil\>

- 
  - 

```
m.names #=> ["x", "y"]

```

- \*/
- 

+static VALUE  
+match\_names(VALUE match)  
+{

- return rb\_reg\_names(RMATCH(match)-\>regexp);  
+}
- 

+/\*

- call-seq:
- 

```
mtch.length => integer

```

- 

```
mtch.size => integer

```

@@ -1362,11 +1476,12 @@ match\_inspect(VALUE match)  
int i;  
int num\_regs = RMATCH(match)-\>regs-\>num\_regs;  
struct backref\_name\_tag \*names;

- VALUE regexp = RMATCH(match)-\>regexp;

- onig\_foreach\_name(RREGEXP(RMATCH(match)-\>regexp)-\>ptr,

- onig\_foreach\_name(RREGEXP(regexp)-\>ptr,  
match\_inspect\_name\_iter, names);

- rb\_define\_method(rb\_cRegexp, “names”, rb\_reg\_names, 0);

- rb\_define\_method(rb\_cRegexp, “named\_captures”,  
rb\_reg\_named\_captures, 0);

- rb\_define\_method(rb\_cMatch, “regexp”, match\_regexp, 0);

- rb\_define\_method(rb\_cMatch, “names”, match\_names, 0);  
rb\_define\_method(rb\_cMatch, “size”, match\_size, 0);  
rb\_define\_method(rb\_cMatch, “length”, match\_size, 0);  
rb\_define\_method(rb\_cMatch, “offset”, match\_offset, 1);  
Index: lib/pp.rb  
===================================================================  
— lib/pp.rb (revision 14161)  
+++ lib/pp.rb (working copy)  
@@ -473,10 +473,24 @@ end

class MatchData  
def pretty\_print(q)

- nc = []
- self.regexp.named\_captures.each {|name, indexes|
- 

```
 indexes.each {|i| nc[i] = name }

```

- }  
q.object\_group(self) {  
q.breakable

- 

```
 q.seplist(1..self.size, lambda { q.breakable }) {|i|

```

- 

```
   q.pp self[i-1]

```

- 

```
 q.seplist(0...self.size, lambda { q.breakable }) {|i|

```

- 

```
   if i == 0

```

- 

```
     q.pp self[i]

```

- 

```
   else

```

- 

```
     if nc[i]

```

- 

```
       q.text nc[i]

```

- 

```
     else

```

- 

```
       q.pp i

```

- 

```
     end

```

- 

```
     q.text ':'

```

- 

```
     q.pp self[i]

```

- 

```
   end
 }

```

}  
end

---

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [December 9, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/regexp-names-matchdata-names-matchdata-regexp/122551/3 "2007-12-09T17:29:27Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32494] Re: Regexp#names, MatchData#names,  
MatchData#regexp”  
on Mon, 10 Dec 2007 00:08:14 +0900, Tanaka A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:  
|  
|In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
| Tanaka A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
|  
|\> named capture e$B$^$o$j$r$=$m$($F$$$F!“$d$O$jL\>A0$N%j%9%H$rF@$ke(B  
|\> e$B%a%=%C%I$,I,MW$@$m$&$H$$$&$3$H$G!“e(BRegexp#names e$B$He(B  
|\> MatchData#names e$B$r\<BAu$7$F$\_$^$7$?$,!”$I$&$G$7$g$&$+!#e(B  
|\>  
|\> e$B$”$H!“e(Bmatch.names e$B$Oe(B match.regexp.names e$B$HF1$8!”$H%I%-%e%a%se(B  
|\> e$B%H$K=q$3$&$H$7$?$N$G$9$,e(B MatchData#regexp e$B$,$J$+$C$?$N$G$3$le(B  
|\> e$B$b$D$1$^$7$?!#e(B  
|  
|e$B$&$%$`!#e(BMatchData#pretty\_print e$B$GL\>A0$b=P$=$&$H;W$C$?$ie(B  
|names e$B$@$1$G$O\>pJs$,B-$j$J$+$C$?$N$G!“e(BRegexp#named\_captures  
|e$B$GL\>A0$+$i%$%s%G%C%/%9$N%j%9%H$X$N%O%C%7%e$rJV$9$h$&$K$7$F$\_e(B  
|e$B$^$7$?!#$3$N7A$O54\<V$,e(B onig\_foreach\_name e$B$GDs6!$7$F$/$l$k$be(B  
|e$B$N$H4pK\E\*$KF1$8$G$9!#e(B  
|(e$B$J$\*!”$3$3$G$be(B MatchData#regexp e$B$,I,MW$K$J$j$^$7$?!#e(B)  
|  
|e$B$I$&$G$7$g$&$+!#e(B

named capturee$B$O\<j$D$+$:$@$C$?$N$G=u$+$j$^$9!#e(B  
[ruby-dev:32493]e$B$H$b$I$b%3%\_%C%H$7$F$/$@$5$$!#e(B
