-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
  • 361
  • 1
  • 10

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,142
  • 4
  • 21
  • 46
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
  • 3
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']
Pradam
  • 773
  • 7
  • 16