If I understood it correctly this should produce the same result, just looks a bit more readable. \w
matches any word-character.
"CN=[\\w]*[., ]+[\\w ]*"
If you want something more flexible you can do this:
Matcher m = Pattern.compile("(CN=.*?),[A-Z]{2}=").matcher(dn);
if (m.find())
cn = m.group(1);
That matches anything between "CN=" and ",XX=", where XX are two uppercase letters. group(1)
returns the first group (the match inside the parentheses).
Regexp's aren't as difficult as they look !