Python 正则表达式 : Find all caps with no following lowercase

标签 python regex

如果后续字符不是小写,我如何保留所有大写字符?

考虑这个例子:

import re
test1 = 'ThisIsATestTHISISATestTHISISATEST'

re.findall(r'[A-Z]{2}[^a-z]+', test1)
# ['THISISAT', 'THISISATEST']

期望: 这:'THISISAT',应该读作:'THISISA'

最佳答案

尝试(regex101):

import re

test1 = "ThisIsATestTHISISATestTHISISATEST"

print(re.findall(r"[A-Z]{2}[A-Z]*(?![a-z])", test1))

打印:

['THISISA', 'THISISATEST']

关于Python 正则表达式 : Find all caps with no following lowercase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73492654/

相关文章:

python - 在 Python 中处理二进制数据

python - 如何使用正则表达式重命名 Pandas DataFrame 的列?

php - MySQL 匹配搜索中的比较运算符

python - 使用 Python 从 KML BatchGeo 文件中提取坐标

python - 值错误 : max() arg is an empty sequence

python - 基于 Flask-Stormpath token 的身份验证

ruby - 在使用正则表达式匹配字符串的递归下降解析器中计算行数?

php - preg_match 所有第一次出现并在文件中获得两个匹配项

c++ - VS2013 : STL regex class crashes when regular expression contains\

python - 列表列表 : replacing and adding up items of sublists