Jeanne Lane
Jeanne Lane

Reputation: 505

How to write a regex that captures where Upper Case ends and Title Case begins?

I have tried both getting the all caps:

[A-Z/:/s/~]*(^[A-Z]{1}[a-z]+) (^ to show not)
[A-Z/:/s/~]*(?![A-Z]{1}[a-z]+)

Or grabbing the sentence case:

[A-Z]{1}[a-z]+ 

[A-Z/:/s/~]*(^[A-Z]{1}[a-z]+) (^ to show not)

Example:

ELIMINATE:TWOS WORD AT THE END OF THIS BUT IGNORE~JUNK~ HELLOTwo Word

How can I get "Two word" out of this

Upvotes: 1

Views: 316

Answers (3)

The fourth bird
The fourth bird

Reputation: 163642

You could match not a lowercase character or a newline [^a-z\n]+ followed by matching an uppercase character [A-Z].

Then capture in a group an uppercase character followed by one or more lowercase characters [A-Z][a-z]+ followed by a repeating pattern (?: [A-Z][a-z]+)* that matches a whitespace and an uppercase character followed by one or more lowercase characters.

The Title Case values are in group 1.

[^a-z\n]+[A-Z]([A-Z][a-z]+(?: [A-Z][a-z]+)*)

Upvotes: 0

QHarr
QHarr

Reputation: 84475

My preferred choice was already given so here is a different one using just the + lookahead.

[A-Z](?=[a-z])\w+

Regex:

regex

Try it

Upvotes: 2

blhsing
blhsing

Reputation: 107134

(?<=[A-Z])([A-Z][a-z]+(?: [A-Z][a-z]+)*) should do.

Demo: https://regex101.com/r/o7zwzG/1

Upvotes: 2

Related Questions