More Regexp help please.

Steve Holden sholden at holdenweb.com
Tue Aug 13 05:46:33 CEST 2002


"SA" <sarmstrong13 at mac.com> 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?
>

I'm assuming that all the strings you want to deal with begin with a
question mark and end in ".htm". If this isn't the case you may need to use
REs, which I tend to save for those times when I *really* can't think of any
other way. (This is because I'm not very good with REs).

Probably you can just get away with using a negative index to specify the
right-hand end of the string slice:

>>> "?1234.htm"[1:-4]
'1234'
>>> "?342.htm"[1:-4]
'342'
>>> "?098.htm"[1:-4]
'098'
>>>

Will this do what you need?

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list