![](/img/trans.png)
[英]extract multiple two consecutive letters from a string using regex in python
[英]Extract lone upper case letters from a string using regex in python
我需要从字符串中提取所有“孤”大写字母。 “单独”是指-不跟在字母数字字符,标点符号,方括号等之前或之后。
因此,对于此输入:
这个L iS A String'A'-T- xxAxx women's(M)
我想要这个输出:
['L', 'A', 'A', 'T', 'M']
我试过了:
re.findall(r'[AZ]',"this L iS A String 'A' -T- xxAxx women's (M)")
但结果是:
['L', 'S', 'A', 'S', 'A', 'T', 'A', 'M']
您需要使用单词边界\\b[AZ]\\b
来仅匹配单独的大写字母。
Python代码
import re
print(re.findall(r'\b[A-Z]\b',"this L iS A String 'A' -T- xxAxx women's (M)"))
打印您的预期输出,
['L', 'A', 'A', 'T', 'M']
如果您希望它与一个或多个大写字母匹配,请使用\\b[AZ]+\\b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.