REGEX: grouping of alternative patterns
- From: stijn@xxxxxxxxxxxxx (Stijn Verholen)
- Date: Tue, 30 Oct 2007 12:07:26 +0100
Hey list,
I'm having problems with grouped alternative patterns.
The regex I would like to use, is the following:
/\s*(`?.+`?)\s*int\s*(\(([0-9]+)\))?\s*(unsigned)?\s*(((auto_increment)?\s*(primary\s*key)?)|((not\s*null)?\s*(default\s*(`.*`|[0-9]*)?)?))\s*/i
It matches this statement:
`id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY
But not this:
`test4` INT(11) UNSIGNED NOT NULL DEFAULT 5
However, if I switch the alternatives, the first statement doesn't match, but the second does.
FYI: In both cases, the column name and data type are matched, as expected.
It appears to be doing lazy evaluation on the pattern, even though every resource I can find states that every alternative is tried in turn until a match is found.
Any ideas on how this can be resolved would be greatly appreciated.
Here is some code:
<?php
$pattern1 = '/\s*(`?.+`?)\s*int\s*(\(([0-9]+)\))?\s*(unsigned)?\s*(((auto_increment)?\s*(primary\s*key)?)|((not\s*null)?\s*(default\s*(`.*`|[0-9]*)?)?))\s*/i';
$pattern2 = '/\s*(`?.+`?)\s*int\s*(\(([0-9]+)\))?\s*(unsigned)?\s*(((not\s*null)?\s*(default\s*(`.*`|[0-9]*)?)?)|((auto_increment)?\s*(primary\s*key)?))\s*/i';
$column1 = '`id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY';
$column2 = '`test4` INT(11) UNSIGNED NOT NULL DEFAULT 5';
$matches = array();
preg_match($pattern1, $column1, $matches); print_r($matches); // match
preg_match($pattern1, $column2, $matches); print_r($matches); // doesn't match
preg_match($pattern2, $column1, $matches); print_r($matches); // doesn't match
preg_match($pattern2, $column2, $matches); print_r($matches); // match
?>
Greetz,
Stijn
.
- Follow-Ups:
- Re: [PHP] REGEX: grouping of alternative patterns
- From: "Robin Vickery"
- Re: [PHP] REGEX: grouping of alternative patterns
- Prev by Date: Re: [PHP] PHP installation
- Next by Date: Re: [PHP] PHP installation
- Previous by thread: PHP installation
- Next by thread: Re: [PHP] REGEX: grouping of alternative patterns
- Index(es):