Python regex find all single alphabetical characters

核能气质少年 提交于 2020-01-15 05:04:31

问题


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

回答1:


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



回答2:


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



回答3:


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".



来源:https://stackoverflow.com/questions/16169815/python-regex-find-all-single-alphabetical-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!