I want to find all indexes for each occurrence of single alphabetical characters in a string. I don't want to catch single char html codes.
Here is my code:
import re
s = "fish oil B stack peanut c "
words = re.finditer('\S+', s)
has_alpha = re.compile(??????).search
for word in words:
if has_alpha(word.group()):
print (word.start())
Desired output:
9
24
解决方案
Using your format (as you wanted) but adding only a simple check.
import re
s = "fish oil B stack peanut c "
words = re.finditer('\S+', s)
has_alpha = re.compile(r'[a-zA-Z]').search
for word in words:
if len(word.group()) == 1 and has_alpha(word.group()):
print (word.start())
>>>
9
24