简体   繁体   中英

Using Java RegEx to find words that start with Upper case and contain lower case

So I am using Java Regex to find acronyms. Most of the Acronyms I am getting are all uppercase, which is fine I have the reg ex for that what I need is a regex for words that will have one lower case letter and the rest are upper case like CCaRS. What Regex could I use for this? Below is a sample of the All upper case Regex I am using.

 String twoPlusUCRegEx = "[A-Z][A-Z]+";

Sample Acronyms: CCaRS, MiDAE

这应该可以工作(以大写字母开头的字母的任意组合,包括2个或多个大写字母):

String twoPlusUCRegEx = "([A-Z][a-z]*){2,}";

The following regex should work:

[A-Z]+[a-z][A-Z]*

Explanation:

[A-Z]+   # one or more uppercase characters
[a-z]    # exactly one lowercase character
[A-Z]*   # zero or more uppercase characters

Note that the body of your text implies that the string doesn't need to start with an uppercase character, but your title says that it does. Please clarify whether or not starting with uppercase is a requirement.

How about:

([A-Z]+[a-z][A-Z]*)|([A-Z]*[a-z][A-Z]+)

If at least 1 uppercase letter is required.

If not:

[A-Z]*[a-z][A-Z]*
String regex = "\\b([A-Z])(\\S*?)\\b";

尝试一下

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM