For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > recursive regex problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author recursive regex problem
Stefan Dreyer

2006-03-10, 6:56 pm

I try to parse following construct:

create table test (
dsdsds int ,
dsds varchar(20)
....
);
I need only the name of the Table "test" and everything inside the outer
paranthese. I tried that with folliwing construct.

// Example from PHP-Documentation
$rec_regexp="(?P<sql>\((((?>[^()]+)|(?P>sql))*)\))";

preg_match("/".$rec_regexp."/ismx",$str,$match);
// matches outer paranthese


preg_match("/create table ([a-zA-Z0-9_]+) ".
$rec_regexp."/ismx",$str,$match);
// Don't match

I tried it with to different PHP-Versions (4.3.8. and 5.1.2).
Does anyone know, how i can match the complete expression?
Ben Bacarisse

2006-03-11, 3:55 am

On Fri, 10 Mar 2006 18:22:08 +0100, Stefan Dreyer wrote:

> I try to parse following construct:
>
> create table test (
> dsdsds int ,
> dsds varchar(20)
> ...
> );
> I need only the name of the Table "test" and everything inside the outer
> paranthese.


This is no possible in any "normal" regular expression. Perl (the
language) has an experimental way to nest a named regex inside another
using (??{ exp }) but I doubt that PHP's regexs have this feature. Feel
free to try it!

Failing that, you need to either:

(a) Read the contents in bits that contain just one (...) pair at a time.
(b) Read it some other way and count the "("s and ")"s so you can stop at
the matching ")".
(c) Find a crude way to tell you are done. Your example has ");" at the
end -- does ")\s;" always end the command? May be the fact the ")" is at
the start of a line will do it.

--
Ben.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com