regex matcher help

ckb

Minion
Staff member
I fail, and I do not know why. It seems my regex is correct, but my matcher results are not what I expect. Maybe I do not understand them. I would expect 3 groups from the matcher pattern, but I get 0.

Can someone help explain where I am going wrong?

Code:
void main ()
{
	string page = "Thus there are numbers of 100 and 85 and 3466 and it was good";
	matcher mm = create_matcher("[0-9]+", page);
	
	if (find(mm))
	{
		for ii from 1 to (group_count(mm))
		{
			print("Found this: "+group(mm,ii),"blue");
		}
	}
	else
	{
		print("not found");
	}
}
 

Veracity

Developer
Staff member
Your matcher did not ask for any groups. However, this:

Code:
void main ()
{
    string page = "Thus there are numbers of 100 and 85 and 3466 and it was good";
    matcher mm = create_matcher("([0-9]+)", page);
	
    while ( find(mm) )
    {
	print("Found this: "+group(mm,1),"blue");
    }
}
yields:

> mm.ash

Found this: 100
Found this: 85
Found this: 3466
 

ckb

Minion
Staff member
Ahhhh... <the light bulbs turns on>

Now it is starting to make more sense. I was misunderstanding the groups. I don't know that I do now, but I understand this enough to get it to work.

Are 'groups' just collections of things inside (brackets)?
So I could create lots of groups with lots of brackets:
Code:
matcher mm = create_matcher("((((([0-9]+)))))", page);
 

Veracity

Developer
Staff member
Yes. They can be more complicated - you can declare that certain patterns inside () don't actually get counted as a group, for example - but in essence, they are as you say. The groups are numbered from 1 to n, counting the "(" from the left end of the pattern.
 
Top