Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > DOTALL not working as expected

Reply
Thread Tools

DOTALL not working as expected

 
 
Stefan Palme
Guest
Posts: n/a
 
      01-18-2007

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-

 
Reply With Quote
 
 
 
 
Stefan Palme
Guest
Posts: n/a
 
      01-18-2007

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-


 
Reply With Quote
 
 
 
 
Roberto Bonvallet
Guest
Posts: n/a
 
      01-18-2007
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
 
Reply With Quote
 
Stefan Palme
Guest
Posts: n/a
 
      01-18-2007

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,


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call __doPostBack from javascript method not working as expected SteveSu ASP .Net 2 11-08-2005 07:03 AM
Cache not working as expected Steve Franks ASP .Net 1 09-11-2005 05:05 AM
Validation not working as expected =?Utf-8?B?amFtbWVy?= ASP .Net 2 08-19-2005 10:21 PM
Re: TabIndex propery not working as expected for Server controls Natty Gur ASP .Net 1 03-03-2004 04:09 PM
GDI+ not working as expected on different server Richard Huff ASP .Net 2 01-06-2004 10:10 PM



Advertisments