繁体   English   中英

Python正则表达式查找所有单个字母字符

[英]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

在最一般的情况下,我会说:

re.compile(r'(?i)(?<![a-z])[a-z](?![a-z])').search

使用外观说“一封字母前面没有另一个字母,后面跟着另一封信”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM