Mr Cricket
Mr Cricket

Reputation: 483

Regular expression to find occurrence of uppercase followed by lowercase

What regular expression can I use for finding occurrences of uppercase followed by lowercase?

For example:

ABcDe

Here I would be hoping to find the 'B' and the 'D'

Upvotes: 1

Views: 7773

Answers (5)

Greg
Greg

Reputation: 1

I believe this is what you are looking for.

([A-Z])[a-z]

Upvotes: 0

ylzhang
ylzhang

Reputation: 1108

In Python:

import re

regex = re.compile("(?:([A-Z])[a-z])")

strdata = 'ABcDefGHIjk'

print [m.group(1) for m in regex.findinter(strdata) if m.group(1) ]

Upvotes: 0

dignitas
dignitas

Reputation: 65

In Javascript, you can do this:

let test_string = "This$IsTheTestForTest_purpose";
test_string.replace(/[\W_]|([a-z])(?=[A-Z])/g, "$1 ")

// console.log(test_string)
// This Is The Test For Test purpose

Upvotes: 0

Dean Harding
Dean Harding

Reputation: 72658

You can use forward lookaheads. You haven't said which "flavour" of regex you're using, so here's a C# example:

var regex = new Regex(@"[A-Z](?=[a-z])");
string str = "ABcDef";
regex.Replace(str, "?");
Console.WriteLine(str); // outputs "A?c?ef"

Additionally, for international characters, you can use Unicode character classes:

var regex = new Regex(@"\p{Lu}(?=\p{Ll})");

Upvotes: 9

Jacob
Jacob

Reputation: 78840

[A-Z][a-z]

(assuming English characters only)

Upvotes: 0

Related Questions