Recursive regular expression (or alternative)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André Hänsel

    Recursive regular expression (or alternative)

    Hi,

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

    blabla(cond1)?{ abc}:{def}blabl a(cond2)?{qwe(c ond3)?{poi}:{io p}}

    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)?{co ntent_when_true }:{content_when _false}
    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é

  • Guest's Avatar

    #2
    Re: Recursive regular expression (or alternative)

    In comp.lang.perl. misc André Hänsel <andre@webkr.de > wrote:

    : Or in words: In a configuration file I have _nested_ conditional blocks
    : in the format (condition)?{co ntent_when_true }:{content_when _false}
    : while the false content ist optional.

    Hint "configurat ion 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: corff@zedat.fu-berlin.de

    Comment

    • fletch

      #3
      Re: Recursive regular expression (or alternative)

      CPAN is Perl not PHP, not sure if previous poster has got his
      newsgroups confused. Everything else the previous poster said applies.

      Comment

      • André Hänsel

        #4
        Re: Recursive regular expression (or alternative)

        fletch schrieb:
        [color=blue]
        > CPAN is Perl not PHP, not sure if previous poster has got his
        > newsgroups confused. Everything else the previous poster said applies.[/color]

        That's ok, the principles probably are the same.


        corff@zedat.fu-berlin.de schrieb:
        [color=blue]
        > Hint "nested": Nested data are preferrably tackled by a parser, not a regex.[/color]

        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é

        Comment

        • fletch

          #5
          Re: Recursive regular expression (or alternative)

          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.

          Comment

          • Tad McClellan

            #6
            Re: Recursive regular expression (or alternative)


            [ Followup set to a newsgroup that I read ]


            André Hänsel <andre@webkr.de > wrote:
            [color=blue]
            > In a configuration file I have _nested_ conditional blocks[/color]
            [color=blue]
            > If you have any completly different solution I'll be glad to hear it,
            > too. :)[/color]


            perldoc -q nest

            How do I find matching/nesting anything?


            --
            Tad McClellan SGML consulting
            tadmc@augustmai l.com Perl programming
            Fort Worth, Texas

            Comment

            Working...