470,848 Members | 1,249 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Recursive regular expression (or alternative)

Hi,

I'm looking for a pattern to parse the following subject:

blabla(cond1)?{abc}:{def}blabla(cond2)?{qwe(cond3) ?{poi}:{iop}}

Probably I can't do that by executing a regexp _once_, so I need some
kind of recursive call to get those two lists (e.g. in an array):
array(
0 => array("cond1","abc","def"),
1 => array("cond2","qwe(cond3)?{poi}:{iop}","")
)
and for the second one then I need the array:
array(
0 => array("cond3","poi","iop")
)

Or in words: In a configuration file I have _nested_ conditional blocks
in the format (condition)?{content_when_true}:{content_when_fals e}
while the false content ist optional.

I built the following pattern, but thats just a bad guess:
/\(([a-zA-Z0-9_ &|]+)\)\?\{(.*)\}(\:\{(.*)\})?/
It matches both blocks when it's greedy and ignores the nested block
when it's ungreedy.

If you have any completly different solution I'll be glad to hear it,
too. :)

Best regards,
André

Apr 12 '06 #1
5 1852
In comp.lang.perl.misc André Hänsel <an***@webkr.de> wrote:

: Or in words: In a configuration file I have _nested_ conditional blocks
: in the format (condition)?{content_when_true}:{content_when_fals e}
: while the false content ist optional.

Hint "configuration file": CPAN has a number of configuration file modules,
did you have a look at them?

Hint "nested": Nested data are preferrably tackled by a parser, not a regex.

: If you have any completly different solution I'll be glad to hear it,
: too. :)

Yes. The XML::Simple module. You have full control over your nested data
structures and can easily evaluate branches of your data conditionally.
Provided, however, that your regex in the beginning didn't describe your
final data format but rather a model of what you want to achieve.

Oliver.

--
Dr. Oliver Corff e-mail: co***@zedat.fu-berlin.de
Apr 13 '06 #2
CPAN is Perl not PHP, not sure if previous poster has got his
newsgroups confused. Everything else the previous poster said applies.

Apr 13 '06 #3
fletch schrieb:
CPAN is Perl not PHP, not sure if previous poster has got his
newsgroups confused. Everything else the previous poster said applies.
That's ok, the principles probably are the same.
co***@zedat.fu-berlin.de schrieb:
Hint "nested": Nested data are preferrably tackled by a parser, not a regex.


Well, how does such a parser look like? Can you give me a hint how to
implement it.

Your other hints don't help me since the format of the file is not to
be changed.

Best regards,
André

Apr 13 '06 #4
Give us some actual data to work with, and what the parser should
produce, your original post hasn't really made that clear to me.

Apr 13 '06 #5

[ Followup set to a newsgroup that I read ]
André Hänsel <an***@webkr.de> wrote:
In a configuration file I have _nested_ conditional blocks If you have any completly different solution I'll be glad to hear it,
too. :)

perldoc -q nest

How do I find matching/nesting anything?
--
Tad McClellan SGML consulting
ta***@augustmail.com Perl programming
Fort Worth, Texas
Apr 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by bortolaso | last post: by
6 posts views Thread by JohnSouth | last post: by
25 posts views Thread by Mike | last post: by
14 posts views Thread by Chris | last post: by
3 posts views Thread by shapper | last post: by
5 posts views Thread by shawnmkramer | last post: by
5 posts views Thread by Noah Hoffman | last post: by
9 posts views Thread by netimen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.