Regex to Split Camelcase or Titlecase (Advanced)

RegEx to split camelCase or TitleCase (advanced)

The following regex works for all of the above examples:

public static void main(String[] args)
{
for (String w : "camelValue".split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}

It works by forcing the negative lookbehind to not only ignore matches at the start of the string, but to also ignore matches where a capital letter is preceded by another capital letter. This handles cases like "VALUE".

The first part of the regex on its own fails on "eclipseRCPExt" by failing to split between "RPC" and "Ext". This is the purpose of the second clause: (?<!^)(?=[A-Z][a-z]. This clause allows a split before every capital letter that is followed by a lowercase letter, except at the start of the string.

Regex to split camel case

My guess is replacing /([A-Z])/ with /([a-z])([A-Z])/ and ' $1' with '$1 $2'

"MyCamelCaseString"
.replace(/([a-z])([A-Z])/g, '$1 $2');

/([a-z0-9])([A-Z])/ for numbers counting as lowercase characters

console.log("MyCamelCaseStringID".replace(/([a-z0-9])([A-Z])/g, '$1 $2'))

How to do CamelCase split in python

As @AplusKminus has explained, re.split() never splits on an empty pattern match. Therefore, instead of splitting, you should try finding the components you are interested in.

Here is a solution using re.finditer() that emulates splitting:

def camel_case_split(identifier):
matches = finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
return [m.group(0) for m in matches]

Regex to split String with uppercase and lowercharacter

This should work:

str.split("(?<=[a-z])(?=[A-Z])")

The idea is to use zero-length lookbehind for a lowercase letter, and zero-length lookahead for the uppercase letter. This construct would match only at the "word breaks" in camel case strings.

Here is a demo on ideone.

Regex to split Camel case - with Numbers

public static void main(String[] args) 
{
for (String w : "camelValue".split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}

Edit: Correcting the case for UPPER2000UPPER the regex becomes:

public static void main(String[] args) 
{
for (String w : "camelValue".split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!(^|[^A-Z]))(?=[0-9])|(?<!(^|[^0-9]))(?=[A-Za-z])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}

Split camelCase word into words with php preg_match (Regular Expression)

You can also use preg_match_all as:

preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);

Explanation:

(        - Start of capturing parenthesis.
(?: - Start of non-capturing parenthesis.
^ - Start anchor.
| - Alternation.
[A-Z] - Any one capital letter.
) - End of non-capturing parenthesis.
[a-z]+ - one ore more lowercase letter.
) - End of capturing parenthesis.

How can I tweak this camelCase regex to allow for acronyms?

I would use this version, which allows for multiple capital letters:

[a-z0-9]+(?:[A-Z0-9]+[a-z0-9]*)*

Here is a demo showing that the pattern is working for you new edge case.

Convert UPPERCASE to Title Case

Many of the useful existing NSString methods are available from Swift. This includes capitalizedString, which may just do exactly what you want, depending on your requirements.



Related Topics



Leave a reply



Submit