Regex for majordomo alias file

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Regex for majordomo alias file

Post 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!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#^(.*?):\s*(.*?,)*(.*?)$#m
:?:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That's what immediately popped into my head as well - just wanted to see if there was a cleaner way.

Thanks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply