我使用下面的正则表达式匹配单词I' m搜索后面的后续单词,在这种情况下,我搜索的单词是test
:
import re
chs = "this is a test Ab Here here big"
print(re.compile(r'test \w+').search(chs).group().split()[1])
从上方Ab
打印。如何修改以返回单词test
后面有大写字母的所有后续单词?
更新:
所以在这种情况下' Ab Here'归还。
答案 0 :(得分:1)
非regex
解决方案会更容易:
chs = "This is A test Ab Here here Big"
index = chs.index('test')
get_all_captial = [val for val in chs[index:].split() if val[0].isupper()]
print(get_all_captial)
# ['Ab', 'Here', 'Big']
答案 1 :(得分:1)
test\s([A-Z].+?)\s[a-z]
匹配Ab Here
this is a test Ab Here here big
答案 2 :(得分:0)
chs = "This is a Test Ab Here here big"
get_all_captial = [val for val in chs.split() if val[0].isupper()]
>>>get_all_captial
['This', 'Test', 'Ab', 'Here']