You can do it like that:
import re
s = " This is a test 1212 test2"
p = re.compile(r"(\b(\w{0,1})\b)|(\b(\d+)\b)")
result = p.sub('', s)
print(result)
Output:
" This is test test2"
I noticed that your desired output does not contain consecutive whitespaces.
If you want to replace consecutive whitespaces by one, you can do this:
p = re.compile(r" +")
result = p.sub(' ', result)
Output:
" This is test test2"
(\b(\w{0,1})\b) this group matches words with length up to 1 (included)
(\b(\d+)\b) this group matches word composed of digit(s) only
| The pipe means "or", so this expression will match either group 1 or group 2
\b It's the "word boundary". By surrounding some regex with "\b", it will match "whole words only"
\w It will match wharacters supposed to part of a word
\d+ This means "at least one digit or more"
Note that what \b and \w will match will depends on the regex flavor you are using.