Replace and inserting strings within .txt files with the use of regex

MRAB python at mrabarnett.plus.com
Mon Aug 9 22:17:25 CEST 2010


Νίκος wrote:
> On 9 Αύγ, 21:05, Thomas Jollans <tho... at jollybox.de> wrote:
>> On Monday 09 August 2010, it occurred to Νίκος to exclaim:
>>
>>> On 9 Αύγ, 19:21, Peter Otten <__pete... at web.de> wrote:
>>>> Νίκος wrote:
>>>>> Please tell me that no matter what weird charhs has inside ic an still
>>>>> open thosie fiels and make the neccessary replacements.
>>>> Go back to 2.6 for the moment and defer learning about unicode until
>>>> you're done with the conversion job.
>>> You are correct again! 3.2 caused the problem, i switched to 2.7 and
>>> now i donyt have that problem anymore. File is openign okey!
>>> it ALMOST convert correctly!
>>> # replace tags
>>> print ( 'replacing php tags and contents within' )
>>> src_data = re.sub( '<\?(.*?)\?>', '', src_data )
>>> it only convert the first instance of php tages and not the rest?
>>> But why?
>> http://docs.python.org/library/re.html#re.S
>>
>> You probably need to pass the re.DOTALL flag.
> 
>  src_data = re.sub( '<\?(.*?)\?>', '', src_data, re.DOTALL )
> 
> like this?

re.sub doesn't accept a flags argument. You can put the flag inside the
regex itself like this:

     src_data = re.sub(r'(?s)<\?(.*?)\?>', '', src_data)

(Note that the abbreviation for re.DOTALL is re.S and the inline flag is
'(?s)'. This is for historical reasons! :-))



More information about the Python-list mailing list