tags:

views:

49

answers:

4

Hi, I'm fairly new to Regex. Hoping someone would help me find a regular expression for finding occurrences of uppercase followed by lowercase. For example:

ABcDe

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

Thanks!

A: 
[A-Z][a-z]

(assuming English characters only)

Jacob
+5  A: 

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})");
Dean Harding
The \p should be lowercase, otherwise it means negation.
Markos
@Markos: oops! Good catch!
Dean Harding
Thanks Dean! This is exactly what I needed. Saved me a lot of time as I was quite stuck here.
Mr Cricket
A: 

I believe this is what you are looking for.

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

Greg
A: 

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) ]

Emacs