match nested parenthesis

Discussion in 'Python' started by s99999999s2003@yahoo.com, Jan 23, 2007.

  1. Guest

    hi
    i wish to find an reg exp for matching nested parenthesis of varying
    level like
    string =
    "somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
    and be able to evaluate the pair starting from the inner most(the
    deepest level) , ie (some)
    up till the outer most. What is a good reg exp to do this? or is simple
    string manipulations enough?
    thanks
     
    , Jan 23, 2007
    #1
    1. Advertising

  2. Terry Reedy Guest

    <> wrote in message
    news:...
    | hi
    | i wish to find an reg exp for matching nested parenthesis of varying
    | level like
    | string =
    | "somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
    | and be able to evaluate the pair starting from the inner most(the
    | deepest level) , ie (some)
    | up till the outer most. What is a good reg exp to do this? or is simple
    | string manipulations enough?
    | thanks

    The presence of indefinitely nested fence markers, like parens,
    differentiates a context-free language, generated from a context-free
    grammar, from a regular language/grammar. You cannot parse such sentences
    with a regex engine or even with an extended regex engine such as Python's.
    Use a more general parser.

    tjr
     
    Terry Reedy, Jan 23, 2007
    #2
    1. Advertising

  3. Neil Cerutti Guest

    On 2007-01-23, <> wrote:
    > hi
    > i wish to find an reg exp for matching nested parenthesis of
    > varying level like
    > string =


    It is not possible, because the set of strings containing
    balanced parenthesis is not regular. Python re's aren't *really*
    regular expressions, so you might be able to hack something up
    using extensions (I'm not sure if it's been proven that you
    can'). I believe regexes are the wrong tool for the job.

    > "somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
    > and be able to evaluate the pair starting from the inner
    > most(the deepest level) , ie (some) up till the outer most.
    > What is a good reg exp to do this? or is simple string
    > manipulations enough?


    Write a context free grammar and a recognizer for that grammar,
    instead. Python developers haven't been able to agree on any such
    module to include in the standard distribution yet,
    unfortunately. But fortunately (consequently), there are tons of
    nice parser and scanner generators available on the net. Try
    pyparsing for a start, as it's a recent, maintained package with
    excellent support on this group at the moment.

    Or just compose your own little function by hand. A stack-based
    solution (implemented using a list as a stack) should be easy enough.

    --
    Neil Cerutti
    And now the sequence of events in no particular order. --Dan Rather

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Neil Cerutti, Jan 23, 2007
    #3
  4. Guest

    wrote:
    > hi
    > i wish to find an reg exp for matching nested parenthesis of varying
    > level like
    > string =
    > "somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
    > and be able to evaluate the pair starting from the inner most(the
    > deepest level) , ie (some)
    > up till the outer most. What is a good reg exp to do this? or is simple
    > string manipulations enough?
    > thanks


    Evaluation using re.sub() and recursion (3 lines of code):

    def eval_sub(expr):
    r"""Evaluate subexpressions in nested paired delimiters.

    For example,
    ‹ single left-pointing angle quotation &lsaquo;
    › single right-pointing angle quotation &rsaquo;

    >>> eval_sub('‹3 * ‹3 + ‹‹1 + 1› * 2›››')
    '21'
    >>> eval_sub('‹3 * 3 + ‹‹1 + 1› * 2›››') # test
    mismatched delimiters
    '13\x9b'
    >>> '\x9b' == '›' # encoding ISO-8859-1
    True
    >>> eval_sub('3 * ‹‹1 + 1› * 2 + 3›') # test absence of
    outer delimiters
    '3 * 7'
    """
    val, n = re.subn("‹([^›‹]+)›", lambda m:
    str(eval(m.group(1))), expr)
    if n == 0:
    return val

    return eval_sub(val)
    #end

    This is just a proof of concept.
     
    , Jan 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    639
  2. Victor
    Replies:
    2
    Views:
    649
    Victor
    May 17, 2004
  3. Gregory Nans
    Replies:
    0
    Views:
    315
    Gregory Nans
    Aug 30, 2003
  4. Carl Cunningham

    Regular expressions and parenthesis in match text

    Carl Cunningham, Sep 15, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    115
    Carl Cunningham
    Sep 15, 2003
  5. Bob
    Replies:
    11
    Views:
    364
    David Combs
    Jul 13, 2004
Loading...

Share This Page