www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - phobos->RegExp

reply "Alexander Panek" <alexander.panek brainsware.org> writes:
Hello,

I got a little problem with the std.regexp.RegExp.match() and .exec()  
methods. Both are just returning the first match of the pattern in the  
string..am i doing something wrong?

std.regexp.RegExp r = std.regexp.search(Source,  
"([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i");
char[][] buf = r.match(src_buf);
while(1)
{
	Attributes ~= new Attribute(r.exec(src_buf)[0], this);
}

hoping for help =\

Regards,
Alex

-- 
huh? did you say something? :o
Apr 04 2005
next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Alexander Panek" <alexander.panek brainsware.org> wrote in message 
news:opsopz4btty2yy8c chello080109082145.3.15.vie.surfer.at...
 Hello,

 I got a little problem with the std.regexp.RegExp.match() and .exec() 
 methods. Both are just returning the first match of the pattern in the 
 string..am i doing something wrong?

 std.regexp.RegExp r = std.regexp.search(Source, 
 "([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i");
 char[][] buf = r.match(src_buf);
 while(1)
 {
 Attributes ~= new Attribute(r.exec(src_buf)[0], this);
 }
I haven't used the regexp module but glancing at the doc r.exec(src_buf) should return an array of matches and then you are indexing only the first element [0] so that is just using the first match. The doc for exec says "returns the next match" but then it says the return is the set of matches so I don't understand what it is actually doing.
Apr 04 2005
prev sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Alexander Panek" <alexander.panek brainsware.org> wrote in message 
news:opsopz4btty2yy8c chello080109082145.3.15.vie.surfer.at...
 Hello,

 I got a little problem with the std.regexp.RegExp.match() and .exec() 
 methods. Both are just returning the first match of the pattern in the 
 string..am i doing something wrong?

 std.regexp.RegExp r = std.regexp.search(Source, 
 "([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i");
 char[][] buf = r.match(src_buf);
 while(1)
 {
 Attributes ~= new Attribute(r.exec(src_buf)[0], this);
 }

 hoping for help =\
I think the doc for regexp is misleading and/or incomplete. I'll add some comments to the wiki about that page. The exec(char[]) function does not return the "next" match. That is what exec() does. The exec() function is not in the online doc but it is a public method of the RegExp class so I assume it is ok to call it. Here is a modified version of your posted example that probably comes closer to what you are looking for: import std.regexp; int main() { char[] Source = "item=\"test\", itemb=\"blah\""; printf("%.*s\n",Source); RegExp r = new RegExp("([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i"); char[][] t = r.exec(Source); printf("%d %.*s %.*s %.*s\n",t.length,t[0],t[1],t[2]); t = r.exec(); printf("%d %.*s %.*s %.*s\n",t.length,t[0],t[1],t[2]); return 0; }
Apr 04 2005
parent "Alexander Panek" <alexander.panek brainsware.org> writes:
On Mon, 4 Apr 2005 19:08:45 -0400, Ben Hinkle <ben.hinkle gmail.com> wrote:

 "Alexander Panek" <alexander.panek brainsware.org> wrote in message
 news:opsopz4btty2yy8c chello080109082145.3.15.vie.surfer.at...
 Hello,

 I got a little problem with the std.regexp.RegExp.match() and .exec()
 methods. Both are just returning the first match of the pattern in the
 string..am i doing something wrong?

 std.regexp.RegExp r = std.regexp.search(Source,
 "([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i");
 char[][] buf = r.match(src_buf);
 while(1)
 {
 Attributes ~= new Attribute(r.exec(src_buf)[0], this);
 }

 hoping for help =\
I think the doc for regexp is misleading and/or incomplete. I'll add some comments to the wiki about that page. The exec(char[]) function does not return the "next" match. That is what exec() does. The exec() function is not in the online doc but it is a public method of the RegExp class so I assume it is ok to call it. Here is a modified version of your posted example that probably comes closer to what you are looking for: import std.regexp; int main() { char[] Source = "item=\"test\", itemb=\"blah\""; printf("%.*s\n",Source); RegExp r = new RegExp("([a-zA-Z]{1,})+=\"+([a-zA-Z0-9]{1,})+\"", "i"); char[][] t = r.exec(Source); printf("%d %.*s %.*s %.*s\n",t.length,t[0],t[1],t[2]); t = r.exec(); printf("%d %.*s %.*s %.*s\n",t.length,t[0],t[1],t[2]); return 0; }
You saved my day, thanks :). Regards, Alex -- huh? did you say something? :o
Apr 05 2005