How to print SRE_Pattern (regexp object) text for debugging purposes?

Discussion in 'Python' started by dmtr, Jun 17, 2010.

  1. dmtr

    dmtr Guest

    I need to print the regexp pattern text (SRE_Pattern object ) for
    debugging purposes, is there any way to do it gracefully? I've came up
    with the following hack, but it is rather crude... Is there an
    official way to get the regexp pattern text?

    >>> import re, pickle
    >>> r = re.compile('^abc$', re.I)
    >>> r

    <_sre.SRE_Pattern object at 0xb7e6a330>

    >>> ds = pickle.dumps(r)
    >>> ds

    "cre\n_compile\np0\n(S'^abc$'\np1\nI2\ntp2\nRp3\n."

    >>> re.search("\n\(S'(.*)'\n", ds).group(1)

    '^abc$'
    >>>


    -- Cheers, Dmitry
     
    dmtr, Jun 17, 2010
    #1
    1. Advertising

  2. dmtr

    MRAB Guest

    Re: How to print SRE_Pattern (regexp object) text for debuggingpurposes?

    dmtr wrote:
    > I need to print the regexp pattern text (SRE_Pattern object ) for
    > debugging purposes, is there any way to do it gracefully? I've came up
    > with the following hack, but it is rather crude... Is there an
    > official way to get the regexp pattern text?
    >
    >>>> import re, pickle
    >>>> r = re.compile('^abc$', re.I)
    >>>> r

    > <_sre.SRE_Pattern object at 0xb7e6a330>
    >
    >>>> ds = pickle.dumps(r)
    >>>> ds

    > "cre\n_compile\np0\n(S'^abc$'\np1\nI2\ntp2\nRp3\n."
    >
    >>>> re.search("\n\(S'(.*)'\n", ds).group(1)

    > '^abc$'
    >


    >>> import re
    >>> r = re.compile('^abc$', re.I)
    >>> r.pattern

    '^abc$'
    >>> r.flags

    2
     
    MRAB, Jun 17, 2010
    #2
    1. Advertising

  3. dmtr

    dmtr Guest

    Re: How to print SRE_Pattern (regexp object) text for debuggingpurposes?

    On Jun 17, 3:35 pm, MRAB <> wrote:
    >
    >  >>> import re
    >  >>> r = re.compile('^abc$', re.I)
    >  >>> r.pattern
    > '^abc$'
    >  >>> r.flags
    > 2



    Hey, thanks. It works.

    Couldn't find it in a reference somehow.
    And it's not in the inspect.getmembers(r).
    Must be doing something wrong.....

    -- Cheers, Dmitry
     
    dmtr, Jun 17, 2010
    #3
  4. dmtr

    MRAB Guest

    Re: How to print SRE_Pattern (regexp object) text for debuggingpurposes?

    dmtr wrote:
    > On Jun 17, 3:35 pm, MRAB <> wrote:
    >> >>> import re
    >> >>> r = re.compile('^abc$', re.I)
    >> >>> r.pattern

    >> '^abc$'
    >> >>> r.flags

    >> 2

    >
    > Hey, thanks. It works.
    >
    > Couldn't find it in a reference somehow.
    > And it's not in the inspect.getmembers(r).
    > Must be doing something wrong.....
    >

    Occasionally you'll find classes whose instances don't reveal all their
    attributes to dir() and so forth, so it's always a good idea to
    double-check the documentation.

    The documentation mentions the .pattern and .flags attributes at:

    http://docs.python.org/library/re.html
    Section 7.2.4. Regular Expression Objects

    (Python 2.6)
     
    MRAB, Jun 18, 2010
    #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. keto
    Replies:
    0
    Views:
    961
  2. Prayag Narula

    Redefine fprintf for debugging purposes

    Prayag Narula, Jul 23, 2007, in forum: C Programming
    Replies:
    16
    Views:
    1,389
    David Thompson
    Aug 26, 2007
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    362
    David Cournapeau
    Dec 30, 2008
  4. John Nagle
    Replies:
    2
    Views:
    346
  5. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
Loading...

Share This Page