match or sub problem?

Alex Martelli aleax at
Mon Sep 23 02:56:13 EDT 2002

jubafre at wrote:

> I have this list of strings:
> final=['20 0x9', '30 0xa', 'B4 0x8', '30 0xb', 'F0', ' 1', ' 3', ' 5']
> and i want to erase the nodes with whitespaces in the begining,

>From the example you give, no, you don't: rather, you want to erase
any whitespace (or a single space? your code suggests the latter --
hard to guess! -- I'll guess the former) at each item's start.

space_at_start = re.compile(r'^\s+')
final2 = [space_at_start.sub('', x) for x in final]

> i use  "match" but doesn?t work and "sub", i think the problem is in the
> "sub", why?

I think your main mistake (out of several you made, such as needlessly
recompiling the RE each time, etc) was not anchoring the match at
start-of-string with the leading ^ (\A would also work here -- the
difference comes only for multiline strings).


More information about the Python-list mailing list