I'm working on a neato Portage project I can't wait to announce, but I've come up against a block in trying to parse atoms in PHP. I've spent the last two hours trying to get the solutions in this thread and others to work with no luck. Most of the other regexes I've seen suggested in other threads and blogs so far seem to lack the completeness required to accurately run through the whole package.mask list. I'm a regex imbecile so I'd greatly appreciate a hand!
My objective is to parse package.mask:
Code: Select all
$masked = file_get_contents("$portage_path/profiles/package.mask");
$masked_lines = explode("\n", $masked);
foreach($masked_lines as $line)
{
$line = trim($line);
if(!empty($line) and substr($line, 0, 1) != '#')
{
$pieces = regexAtom($line);
print_r($pieces);
}
}
- operator
- category
- package
- version
- range
I started off with a brute-force if(substr()) strategy, making ample use of explode() but it all fell apart when it came to package names with multiple hyphens and version numbers with revisions and the like (i.e. -2.10-r1)
The closest I've come to workable output with a regex is the linked thread's last poster's patter, but it spits out a bunch of mostly empty arrays, most of the time. The arrays that are populated have repeated values and other flaws. For example:
Code: Select all
function regexAtom($atom)
{
preg_match_all("/^([<>]?=?)(([^\/]+)\/)?(?U)(\S+)(-(\d+(\.\d+)*[a-z]?(_(alpha|beta|pre|rc|p)\d*)*(-r\d+)?)?)$/", $atom, $matches);
return $matches;
}
...
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
[5] => Array
(
)
[6] => Array
(
)
[7] => Array
(
)
[8] => Array
(
)
[9] => Array
(
)
[10] => Array
(
)
)
Array
(
[0] => Array
(
[0] => >=app-i18n/scim-anthy-1.3.0
)
[1] => Array
(
[0] => >=
)
[2] => Array
(
[0] => app-i18n/
)
[3] => Array
(
[0] => app-i18n
)
[4] => Array
(
[0] => scim-anthy
)
[5] => Array
(
[0] => -1.3.0
)
[6] => Array
(
[0] => 1.3.0
)
[7] => Array
(
[0] => .0
)
[8] => Array
(
[0] =>
)
[9] => Array
(
[0] =>
)
[10] => Array
(
[0] =>
)
)




