Rich McCluskey
Rich McCluskey

Reputation: 2011

Regex to match words and acronyms in camel case string

I need a regular expression to capture the word parts of a string that is camel case and might have an acronym in it. In other words, I want to split a camel case string into its words and acronyms.

For example:

SomeABCWords

... has three capture groups

Some ABC Words

So far I've found this regex:

((?:^|[A-Z])[a-z]+)

But that won't handle the acronyms and would just match 'Some' and 'Words'.

Upvotes: 1

Views: 1108

Answers (2)

Kewin Dousse
Kewin Dousse

Reputation: 4027

This regex should work, at least for the case you posted :

[A-Z][a-z]+|[A-Z]*(?![a-z])

Upvotes: 0

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

One way to solve it is to capture abbreviations with added negative lookahead.

[A-Z][a-z]+|[A-Z]+(?![a-z])

Regular expression visualization

Sample

Upvotes: 3

Related Questions