Seebs wrote:

> I have an existing hunk of Makefile code:
> CPPFLAGS = "$(filter -D* -I* -i* -U*,$(TARGET_CFLAGS))"
> For those not familiar with GNU makeisms, this means "assemble a string
> which consists of all the words in $(TARGET_CFLAGS) which start with one
> of -D, -I, -i, or -U".  So if you give it
> foo -Ibar baz
> it'll say
> -Ibar
> 
> I have a similar situation in a Python context, and I am wondering
> whether this is an idiomatic spelling:
> 
> ' '.join([x for x in target_cflags.split() if re.match('^-[DIiU]', x)])
> 
> This appears to do the same thing, but is it an idiomatic use of list
> comprehensions, or should I be breaking it out into more bits?

You may be able split and match with a single regex, e. g.

>>> cflags = "-Dxxx -D zzz -i- -U42-U7 -i -U"

>>> re.compile(r"-[DIiU]\S*").findall(cflags)
['-Dxxx', '-D', '-i-', '-U42-U7', '-i', '-U']

>>> re.compile(r"-[DIiU]\s*(?:[^-]\S*)?").findall(cflags)
['-Dxxx', '-D zzz', '-i', '-U42-U7', '-i ', '-U']

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to