我想查找字符串中每次出现的单个字母字符的所有索引。我不想捕获单个字符的 html 代码。
这是我的代码:
import re
s = "fish oil B stack peanut c <b>"
words = re.finditer('\S+', s)
has_alpha = re.compile(??????).search
for word in words:
if has_alpha(word.group()):
print (word.start())
期望的输出:
9
24
最佳答案
这样做:
r'(?i)\b[a-z]\b'
分解:
- 不区分大小写的匹配
- 单词边界
- 一封信
- 单词边界
您的代码可以简化为:
for match in re.finditer(r'(?i)\b[a-z]\b', s):
print match.start()
关于Python 正则表达式查找所有单个字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16169815/