monocular
monocular

Reputation: 323

Match the phrase that have first char capitalized

I want to match the phrase that have every first char capitalized before the word 'users'.

For example:

I've a string : to Apple Tablet users or anything for Apple Tablet users

The result after matching must be : Apple Table

or anything-without-first-char-capitalized-here Abc users => Abc

or anything-without-first-char-capitalized-here Abc Def users => Abc Def

or anything-without-first-char-capitalized-here Abc Def Xyz users => Abc Def Xyz

What i've try : (((?-i)[A-Z][a-z]*) )*users But it's only matching the first word - Tablet

Upvotes: 0

Views: 66

Answers (3)

Exception
Exception

Reputation: 8389

This should work

(([A-Z]+[\w-]*[\s])+users)

Matches are below

Explanation here

Upvotes: 0

Daniel Gimenez
Daniel Gimenez

Reputation: 20689

You can use quantification to match multiple upper case words. Then use a look ahead to find users but not include it in the result.

(?:\b[A-Z][a-z]*\s*)+(?=\busers\b)

REY

REY has a working example with all of your sample data.

Upvotes: 1

Jerry
Jerry

Reputation: 71598

It's just a matter of correctly grouping:

((?:(?-i)[A-Z][a-z]* )*)users

You could perhaps do this if you don't want to get the last space in the capture:

((?:(?-i)[A-Z][a-z]* ??)*?)\s*users

regex101 demo


(((?-i)[A-Z][a-z]*) )*
^^----------------^ ^
|_________1_________|
          2

The first (inner) capture deals with the words, the second (outer) capture adds the space. You can easily combine the two in a single group (hence why you find (?:(?-i)[A-Z][a-z]* ) in my regex.

Then the issue is that the last capture is being repeated and this is what causes only Tablet to be capture in the first capture group. Compare with how the ultimate capture group is not repeated in the regex I suggested.

Upvotes: 1

Related Questions