简体   繁体   English

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

[英]Python regex find all single alphabetical characters

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. 我不想捕获单个char html代码。

Here is my code: 这是我的代码:

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())

Desired output: 期望的输出:

9
24

This does it: 这样做:

r'(?i)\b[a-z]\b'

Breaking it down: 打破它:

  • Case insensitive match 不区分大小写的匹配
  • A word boundary 单词边界
  • A letter 一封信
  • A word boundary 单词边界

Your code can be simplified to this: 您的代码可以简化为:

for match in re.finditer(r'(?i)\b[a-z]\b', s):
   print match.start()

Using your format ( as you wanted ) but adding only a simple check. 使用您的格式( 如您所愿 ),但只添加一个简单的检查。

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

In the most general case I'd say: 在最一般的情况下,我会说:

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

Using lookarounds to say "a letter not preceded by another letter nor followed by another letter". 使用外观说“一封字母前面没有另一个字母,后面跟着另一封信”。

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

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