note 62221 added to function.preg-match
| From: | jpittman2 at gmail dot com | Date: | Wed, 22 Feb 2006 17:08:50 +0000 |
| Subject: | note 62221 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104234@lists.php.net to get a copy of this message | ||
Here's an extremely complicated regular expression to match the various parts of an Oracle 8i
SELECT statement. Obviously the SELECT statment is contrived, but hopefully this RegExp will work
on just about any valid SELECT statement. If there are any problems, feel free to comment.
<?
$sql = "SELECT /*+ ORDERED */ UNIQUE foo, bar, baz FROM fly, flam"
. " WHERE bee=1 AND fly=3"
. " START WITH foo=1 CONNECT BY some_condition"
. " CONNECT BY some_other_condition"
. " GROUP BY a_bunch_of_fields HAVING having_clause"
. " CONNECT BY some_other_connect_by_condition"
. " INTERSECT (SELECT * FROM friday WHERE beep=1)"
. " ORDER BY one, two, three"
. " FOR UPDATE bee bop boo";
$re = "/^ # Match beginning of string
SELECT\\s+ # SELECT
(?:(?P<hints>\\/\\*\\+\\s+.+\\s+\\*\\/|--\\+\\s+.+\\s+--)\\s+)? # Hints
(?:(?P<DUA>DISTINCT|UNIQUE|ALL)\\s+)? # (self-explanatory)
(?P<fields>.+?) # fields
\\s+FROM\\s+ # FROM
(?:(?P<tables>.+?) # tables
(?:\\s+WHERE\\s+(?P<where>.+?))? # WHERE Clauses
(?:\\s+
(?:(?:START\\s+WITH\\s(?P<startWith>.+?)\\s+)? # START WITH
CONNECT\\s+BY\\s+(?P<connectBy>.+?) # CONNECT BY
) # Hierarchical Query
|
(?:GROUP\\s+BY\\s+(?P<groupBy>.+?) # Group By
(?:\\s+HAVING\\s+(?P<having>.+?))?) # Having
)* # Hier,Group
(?:\\s+(?P<UIM>UNION(?:\\s+ALL)?|INTERSECT|MINUS) # UNION,INTSECT,MINUS
\\s+\\((?P<subquery>.+)\\))? # UIM subquery
(?:\\s+ORDER\\s+BY\\s+(?P<orderBy>.+?))? # Order by
(?:\\s+FOR\\s+UPDATE\\s*(?P<forUpdate>.+?)?)? # For Update
) # tables
$ # Match end of string
/xi";
$matches = array();
preg_match($re, $sql, $matches);
var_dump($matches);
?>
----
Server IP: 216.194.113.175
Probable Submitter: 199.46.198.232 (proxied: 138.127.192.150)
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62221
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62221&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62221&report=yes
Search -- http://master.php.net/manage/user-notes.php