More Regexp help please.

John La Rooy igetenoughspamalreadythanksjlr at doctor.com
Mon Aug 12 11:27:52 EDT 2002


SA wrote:
> Hi Everyone-
> 
> I am trying to match a string pattern like the following:
> 
> ?1234.htm
> 
> I would then like to extract the 1234 from the pattern and sub 1234.html for
> the pattern. Of course there will be more than one match per file and 1234
> will be variable from match to match. But the ? And .htm will be in each
> pattern. For example:
> 
> ?1234.htm
> ?342.htm
> ?098.htm
> 
> 
> Anyone know how to do this? Is there a simpler way than re?
> 
> Thanks.
> SA
> 
> 
This should get you started

re.sub("\?(\d+).htm","\\1.html","?1234.htm")

\?    matches the ?
(\d+) matches 1 or more digits and is substituted for the \\1

John




More information about the Python-list mailing list