简体   繁体   中英

Regular Expression for splitting a String by Upper Case only words in Java

I have this...

 String bigWord = "I AM LOUD But also sensitive";
 String[] words = bigWord.split("[^A-Z| ]");

I want the first entry in that list to be 'I AM LOUD' and the second to be 'But also sensitive'.

The above RegEx almost works but it captures the first letter of the second entry..

"I AM LOUD B"

How can I fix that?

Use Matcher class so you can get the groups easily.

      String bigWord = "I AM LOUD But also sensitive";

      Pattern pattern = Pattern.compile("([A-Z ]+)? ([A-Z]?.*)");
      Matcher matcher = pattern.matcher(bigWord);
      while(matcher.find()){
         System.out.println(matcher.group(1));
         System.out.println(matcher.group(2));
      }

Output:

I AM LOUD
But also sensitive

The key in the regexp is:

A group of uppercased words an space A group of mixed chars

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