Reputation: 53806
i'm attempting to extract the word 'Here' as 'Here' contains a capital letter at beginning of word and occurs before word 'now'.
Here is my attempt based on regex from :
regex match preceding word but not word itself
import re
sentence = "this is now test Here now tester"
print(re.compile('\w+(?= +now\b)').match(sentence))
None is printed in above example.
Have I implemented regex correctly ?
Upvotes: 0
Views: 884
Reputation: 9019
The following works for the given example:
Regex:
re.search(r'\b[A-Z][a-z]+(?= now)', sentence).group()
Output:
'Here'
Explanation:
\b
imposes word boundary
[A-Z]
requires that word begins with capital letter
[a-z]+
followed by 1 or more lowercase letters (modify as necessary)
(?= now)
positive look-ahead assertion to match now
with leading whitespace
Upvotes: 4