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
&& (And) and || (Or) in If Statements
H2 In-Memory Database. Table Not Found
"Java.Lang.Outofmemoryerror:Unable to Create New Native Thread"
Jtable How to Refresh Table Model After Insert Delete or Update the Data
How to Add Hyperlink in Jlabel
El Access a Map Value by Integer Key
How to Exclude All Instances of a Transitive Dependency When Using Gradle
How to Find Nth Occurrence of Character in a String
What Causes "Unable to Access Jarfile" Error
What Is the Regex to Extract All the Emojis from a String
Ideal Method to Truncate a String with Ellipsis
Why Does the First Panel Added to a Frame Disappear
Add an Background Image to a Panel
Differencebetween Jvm, Jdk, Jre & Openjdk
Creation Timestamp and Last Update Timestamp with Hibernate and MySQL