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

Νίκος nikos.the.gr33k at gmail.com
Mon Aug 9 10:21:07 EDT 2010


On 9 Αύγ, 16:52, MRAB <pyt... at mrabarnett.plus.com> wrote:
> Νίκος wrote:
> > On 8 Αύγ, 17:59, Thomas Jollans <tho... at jollans.com> wrote:
>
> >> Two problems here:
>
> >> str.replace doesn't use regular expressions. You'll have to use the re
> >> module to use regexps. (the re.sub function to be precise)
>
> >> '.'  matches a single character. Any character, but only one.
> >> '.*' matches as many characters as possible. This is not what you want,
> >> since it will match everything between the *first* <? and the *last* ?>.
> >> You want non-greedy matching.
>
> >> '.*?' is the same thing, without the greed.
>
> > Thanks you,
>
> > So i guess this needs to be written as:
>
> > src_data = re.sub( '<?(.*?)?>', '', src_data )
>
> In a regex '?' is a special character, so if you want a literal '?' you
> need to escape it. Therefore:
>
>      src_data = re.sub(r'<\?(.*?)\?>', '', src_data)

i see, or perhaps even this:

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

maybe it works here as well.



More information about the Python-list mailing list