Q:

Splitting string into words with regex

How can I split string with regex into words not longer than 3 characters like:
Input
"ads1323z123123c123123890sdfakslk123klaad,313ks"

Output
['ads', 'z', 'c', 'ks']

A:

You can also use lookahead and lookbehind expressions to match only 3-character words:
import re
s = "ads1323z123123c123123890sdfakslk123klaad,313ks"
re.findall('(?<![a-zA-Z])[a-zA-Z]{1,3}(?![a-zA-Z])', s)

Output:
['ads', 'z', 'c', 'ks']

The regular expression works like this: the middle part [a-zA-Z]{1,3} says "match 1 to 3 alphabetic characters". The first part (?<![a-z][A-Z]) is a negative lookbehind assertion that asserts that the 3 alphabetic characters are not preceded by an alphabetic character.  the last part (?![a-zA-Z]) is a negative lookahead assertion that asserts that the 3 alphabetic characters are not followed by an alphabetic character.

