Reputation: 323
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
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 has a working example with all of your sample data.
Upvotes: 1
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
(((?-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