re module methods: flags(), pattern()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    re module methods: flags(), pattern()

    Python re module has methods flags and pattern. How to use these
    exactly?

    e.g. i tried

    print patternObj.flag s()

    and the error is some "int object is not callable".

    newpattern=re.c ompile(ur'\w+', patternObj.flag s())

    also bad.

    similar error for patternObj.patt ern(). (and i suppose the same for
    groupindex() )

    thanks.

    Xah
    xah@xahlee.org
    ∑ http://xahlee.org/

  • Reinhold Birkenfeld

    #2
    Re: re module methods: flags(), pattern()

    Xah Lee wrote:[color=blue]
    > Python re module has methods flags and pattern. How to use these
    > exactly?
    >
    > e.g. i tried
    >
    > print patternObj.flag s()
    >
    > and the error is some "int object is not callable".[/color]

    Where do you read "methods"?

    Reinhold

    Comment

    • Kay Schluehr

      #3
      Re: re module methods: flags(), pattern()

      Xah Lee wrote:[color=blue]
      > Python re module has methods flags and pattern. How to use these
      > exactly?[/color]
      [color=blue]
      >From the Python 2.3 documentation, section 2.4.2[/color]

      flags

      The flags argument used when the RE object was compiled, or 0 if no
      flags were provided.


      groupindex

      A dictionary mapping any symbolic group names defined by (?P<id>) to
      group numbers. The dictionary is empty if no symbolic groups were used
      in the pattern.

      pattern

      The pattern string from which the RE object was compiled.


      Comments derived from the docs and error messages:
      [color=blue]
      > e.g. i tried
      >
      > print patternObj.flag s()
      >
      > and the error is some "int object is not callable".[/color]

      => flags is an int object
      [color=blue]
      >
      > newpattern=re.c ompile(ur'\w+', patternObj.flag s())
      >
      > also bad.[/color]

      => flags is still an int object

      [color=blue]
      > similar error for patternObj.patt ern().[/color]

      => pattern is a string
      [color=blue]
      > (and i suppose the same for
      > groupindex() )[/color]

      => groupindex is a dict
      [color=blue]
      >
      > thanks.
      >
      > Xah
      > xah@xahlee.org
      > ∑ http://xahlee.org/[/color]

      The documentation is not that bad that one should not read it at all ;)


      If You wrestle with regexps I can recommend an old but still usefull
      little wrapper I found eventually on the "Vaults of Parnassus" which is
      called reverb.py

      Ciao,
      Kay

      Comment

      • André Søreng

        #4
        Re: re module methods: flags(), pattern()


        A flag is just an int. From the re doc, you can see
        there is a ignorecase flag:

        "
        I
        IGNORECASE
        Perform case-insensitive matching; expressions like [A-Z] will
        match lowercase letters, too. This is not affected by the current locale.
        "

        Using the ignorecase flag:
        [color=blue][color=green][color=darkred]
        >>> import re
        >>> print re.I[/color][/color][/color]
        2[color=blue][color=green][color=darkred]
        >>> print re.IGNORECASE[/color][/color][/color]
        2[color=blue][color=green][color=darkred]
        >>> r = re.compile("hel lo", re.IGNORECASE)
        >>> print r.pattern[/color][/color][/color]
        'hello'[color=blue][color=green][color=darkred]
        >>> match = r.search("heLLo ")
        >>> match.group()[/color][/color][/color]
        'heLLo'

        As the doc says, you can combine several flags with the '|' operator:
        r = re.compile("you r_regexp", re.SOMEFLAG|re. SOMEOTHERFLAG)


        Xah Lee wrote:[color=blue]
        > Python re module has methods flags and pattern. How to use these
        > exactly?
        >
        > e.g. i tried
        >
        > print patternObj.flag s()
        >
        > and the error is some "int object is not callable".
        >
        > newpattern=re.c ompile(ur'\w+', patternObj.flag s())
        >
        > also bad.
        >
        > similar error for patternObj.patt ern(). (and i suppose the same for
        > groupindex() )
        >
        > thanks.
        >
        > Xah
        > xah@xahlee.org
        > ∑ http://xahlee.org/
        >[/color]

        Comment

        Working...