Page 1 of 1

Regex for majordomo alias file

Posted: Fri Feb 03, 2006 4:59 pm
by pickle
Hi all,

I've been charged with the task of parsing througha Majordomo alias file and pulling out all aliases. The format of the lines I'm concerned with is:

<alias name>:<whitespace> [ [<entry>,{0,1}]* | <entry>]

Which basically means I could have either:
aliasname: entry1, entry2, entry3
-OR-
aliasname: entry

I've got my expression to the point where I can pull out the alias name and all the entries as a single string (ie: 'entry1,entry2,entry3'). That expression is:

Code: Select all

/(.*?):\s*(.*)/
Now, I want to see if I can modify that expression so that it also pulls out EACH entry from the list if it exists. So, I'd like preg_match() to give me

[0] server: entry1,entry2,entry3
[1] server
[2] entry1
[3] entry2
[4] entry3

I *think* this can be accomplished with looking ahead or looking back in the expression, but it's honestly out of my mental capacity to figure out how. I was thinking this might work, but I was proven wrong:

Code: Select all

/(.*?):\s*(?:(.*?),*)*/

If anyone has any ideas, it would be greatly appreciated. Or, if you know this can't be done, please let me know and I'll do it an alternative way.

Thanks folks!

Posted: Fri Feb 03, 2006 5:39 pm
by feyd

Code: Select all

#^(.*?):\s*(.*?,)*(.*?)$#m
:?:

Posted: Fri Feb 03, 2006 6:04 pm
by pickle
feyd wrote:

Code: Select all

#^(.*?):\s*(.*?,)*(.*?)$#m
:?:
Best effor so far, but that only returns the last two entries in the list. Reading through the regex, it's probably because you've just got two parentheses at the end. Is there a way to *loop* through a part of a regex somehow?

Posted: Fri Feb 03, 2006 8:47 pm
by feyd
I'd probably use a two pass setup for this. One that extracts the alias name and the entries, and one that breaks the entries apart.

Code: Select all

preg_match_all('#^\s*(.*?)\s*:\s*(.*?)\s*$#m',$text,$matches);

// ...

$entries = array_map('trim',explode(',',$entry));

Posted: Mon Feb 06, 2006 9:40 am
by pickle
That's what immediately popped into my head as well - just wanted to see if there was a cleaner way.

Thanks.