Finding letters in string, not followed by a number... possibly using RE?

python, regex, string

Solution

Use negative look-arounds:

re.findall(r"(?<![\da-z])[a-z]+(?![\da-z])", string.lower())

This matches lower-case letters that are not immediately preceded or followed by more letters or digits.

Demo:

>>> import re
>>> string = "ts0060_LOD-70234_lr2_billboards_rgba_over_s3d_lf_v5_2Kdciufa_lnh"
>>> re.findall(r"(?<![\da-z])[a-z]+(?![\da-z])", string.lower())
['lod', 'billboards', 'rgba', 'over', 'lf', 'lnh']

Problem

I am trying to extract letters from a string, which do not follow, or are not followed directly by a number. Here's an example string: ``` string = "ts0060_LOD-70234_lr2_billboards_rgba_over_s3d_lf_v5_2Kdciufa_lnh" ``` This is what I have so far: ``` re.findall(r"[a-z]+", string.lower()) ``` which gives this result: ``` ['ts', 'lod', 'lr', 'billboards', 'rgba', 'over', 's', 'd', 'lf', 'v', 'kdciufa', 'lnh'] ``` ... but the result I am looking for is something more like this: ``` ['lod', 'billboards', 'rgba', 'over', 'lf', 'lnh'] ``` Is there a way of achieving this using regular expressions? Many thanks,

Original source