DOTALL not working as expected

Discussion in 'Python' started by Stefan Palme, Jan 18, 2007.

  1. Stefan Palme

    Stefan Palme Guest

    Hi all,

    using the "re" module of Python (2.3 and 2.4), I tried the following:

    import re
    print re.sub('X.*?Y', 'Z', 'Xab\ncdY', re.DOTALL)

    I wanted to replace
    Xab
    cdY
    by a single "Z", but the "." in the pattern does not match the
    included "\n".

    When using the pattern "X(.|\n)*?Y" (explicity including "\n"
    in the set of "any character") I get the wanted result.

    My fault or a bug in the module?

    Thanks
    -stefan-
     
    Stefan Palme, Jan 18, 2007
    #1
    1. Advertising

  2. Stefan Palme

    Stefan Palme Guest

    Just noticed, that it works when *compiling* the pattern:

    import re
    p = re.compile('X.*?Y', re.DOTALL)
    print re.sub(p, 'Z', 'Xab\ncdY')

    Still the question - my fault or a bug?

    Best regards
    -stefan-


    On Thu, 18 Jan 2007 11:10:08 +0100, Stefan Palme wrote:
    >
    > Hi all,
    >
    > using the "re" module of Python (2.3 and 2.4), I tried the following:
    >
    > import re
    > print re.sub('X.*?Y', 'Z', 'Xab\ncdY', re.DOTALL)
    >
    > I wanted to replace
    > Xab
    > cdY
    > by a single "Z", but the "." in the pattern does not match the
    > included "\n".
    >
    > When using the pattern "X(.|\n)*?Y" (explicity including "\n"
    > in the set of "any character") I get the wanted result.
    >
    > My fault or a bug in the module?
    >
    > Thanks
    > -stefan-
     
    Stefan Palme, Jan 18, 2007
    #2
    1. Advertising

  3. Stefan Palme wrote:
    >> using the "re" module of Python (2.3 and 2.4), I tried the following:
    >>
    >> import re
    >> print re.sub('X.*?Y', 'Z', 'Xab\ncdY', re.DOTALL)
    >>

    > Just noticed, that it works when *compiling* the pattern:
    >
    > import re
    > p = re.compile('X.*?Y', re.DOTALL)
    > print re.sub(p, 'Z', 'Xab\ncdY')
    >
    > Still the question - my fault or a bug?


    Your fault. According to the documentation [1], the re.sub function takes
    a count as a fourth argument, not the compilation flags.

    [1] http://docs.python.org/lib/node46.html

    Cheers,
    --
    Roberto Bonvallet
     
    Roberto Bonvallet, Jan 18, 2007
    #3
  4. Stefan Palme

    Stefan Palme Guest

    arghh.
    Thanks for removing my blindness :)
    -stefan-

    On Thu, 18 Jan 2007 10:38:35 +0000, Roberto Bonvallet wrote:
    > Stefan Palme wrote:
    >>> using the "re" module of Python (2.3 and 2.4), I tried the following:
    >>>
    >>> import re
    >>> print re.sub('X.*?Y', 'Z', 'Xab\ncdY', re.DOTALL)
    >>>

    >> Just noticed, that it works when *compiling* the pattern:
    >>
    >> import re
    >> p = re.compile('X.*?Y', re.DOTALL)
    >> print re.sub(p, 'Z', 'Xab\ncdY')
    >>
    >> Still the question - my fault or a bug?

    >
    > Your fault. According to the documentation [1], the re.sub function takes
    > a count as a fourth argument, not the compilation flags.
    >
    > [1] http://docs.python.org/lib/node46.html
    >
    > Cheers,
     
    Stefan Palme, Jan 18, 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. Richard Huff
    Replies:
    2
    Views:
    421
    Richard Huff
    Jan 6, 2004
  2. Natty Gur
    Replies:
    1
    Views:
    1,921
    Marshal Antony
    Mar 3, 2004
  3. =?Utf-8?B?amFtbWVy?=

    Validation not working as expected

    =?Utf-8?B?amFtbWVy?=, Aug 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    419
    =?Utf-8?B?amFtbWVy?=
    Aug 19, 2005
  4. Steve Franks

    Cache not working as expected

    Steve Franks, Sep 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    475
    Jeremy S.
    Sep 11, 2005
  5. SteveSu
    Replies:
    2
    Views:
    17,202
    SteveSu
    Nov 8, 2005
Loading...

Share This Page