Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Regex group bug?

Moderators: kris

Posted: 05/09/08 13:47:01

I cannot get groups to work at all.

Se this simple example:

	char[] txt = "asdf@1234";
	auto r = Regex("^([a-z]+)@([0-9]+)$");
	char[] txt = "asdf@1234";
	auto r = Regex("^([a-z]+)@([0-9]+)$");

	r.test(txt);
	Stdout.formatln("Pre: {} Match(0): {} Post: {}",
			r.pre, r.match(0), r.post);

	Stdout.formatln("Split: {}", r.split(txt));

Output:

Pre:  Match(0): asdf@1234 Post:
Split: [ ,  ]

I would have thought I could loop through the matches and get:

Pre:  Match(0): asdf Post: @1234
Pre: asdf@ Match(1): 1234 Post: 
Author Message

Posted: 08/31/08 14:58:20

I don't know if you still have problems since I'm looking at older posts.

You're missing one simple point, match(0) goes for whole matched expression, and that would be whole string in your case. try this:

import tango.io.Stdout;
import tango.text.Regex;

void main()
{
    char[] txt = "XXXasdf@1234---";
    auto r = Regex(r"([a-z]+)@([0-9]+)");

    r.test(txt);
    Stdout.formatln("Pre: {} ", r.pre)
        .formatln(" (whole match) Match(0): {} ", r[0])
        .formatln(" Match(1): {} ", r[1])
        .formatln(" Match(2): {} ", r[2])
        .formatln(" Post: {}", r.post);

    Stdout.formatln("Split: {}", r.split(txt));
}


You know Papaya, don't you?