-1

I use below regex to match the subsequent word after word I'm searching for, in this case the word I'm searching for is test :

import re

chs = "this is a test Ab Here here big"

print(re.compile(r'test \w+').search(chs).group().split()[1])

From above Ab is printed. How can I modify to return all subsequent words that have capital letter subsequent to word test ?

Update :

So in this case 'Ab Here' is returned.

thepen
  • 371
  • 1
  • 11

3 Answers3

1

A non regex solution would be easier:

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']
Austin
  • 25,759
  • 4
  • 25
  • 48
1

test\s([A-Z].+?)\s[a-z]

matches Ab Here in this is a test Ab Here here big

See: https://regex101.com/r/jYZucl/1

nnamdi
  • 101
  • 4
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']
Veera Balla Deva
  • 790
  • 6
  • 19