Match words beginning with an uppercase letter until a word beginning with an uppercase letter

I would like to match words in a line that start with an uppercase letter and continue until the word (s) that begin with the other uppercase letter begin.

Animals Chicken helmet Animal baby kids Crazy 6 yo monkey Baby white-black horse

Match: / contains lowercase letters, numbers, special characters

Animals Chicken helmet Animal baby kids Crazy 6 yo monkey Baby white-black horse

i tried this

(^[A-Za-z]+\s?\w+\W+?\d?)+

Upvotes: 4

Views: 73

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

You can use

\b[A-Z]\S*(?:\s+[^A-Z\s]\S*)*

See the regex demo.

Details

  • \b - a word boundary (if the words are separated with whitespaces, replace with (?<!\S))
  • [A-Z]\S* - an uppercase ASCII letter (replace with \p{Lu} if supported for Unicode support) and then any zero or more non-whitespace chars
  • (?:\s+[^A-Z\s]\S*)* - zero or more occurrences of
    • \s+ - one or more whitespaces
    • [^A-Z\s] - any char but whitespace and an uppercase ASCII letter (use [^\p{Lu}\s] if Uniocde support is necessary and is supported)
    • \S* - any zero or more non-whitespace chars

Upvotes: 3

Related Questions