Rich McCluskey Rich McCluskey - 3 months ago 18
PHP Question

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'.

Answer

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

Comments