[英]Python regex find all single alphabetical characters
我想查找字符串中每次出现的单个字母字符的所有索引。 我不想捕获单个char 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()
使用您的格式( 如您所愿 ),但只添加一个简单的检查。
import re
s = "fish oil B stack peanut c <b>"
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.