Hossein
Hossein

Reputation: 41911

How to extract uppercase substrings from a String in Java?

I need a piece of code with which I can extract the substrings that are in uppercase from a string in Java. For example:

"a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]"

I need to extract CC BBBBBBB and AAAA

Upvotes: 4

Views: 4783

Answers (5)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19847

You can do it with String[] split(String regex). The only problem can be with empty strings, but it's easy to filter them out:

String str = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]";
String[] substrings = str.split("[^A-Z]+");
for (String s : substrings)
{
    if (!s.isEmpty())
    {
        System.out.println(s);
    }
}

Output:

AAAA
BBBBBBB
CC

Upvotes: 6

Andrei Suceava
Andrei Suceava

Reputation: 307

This is probably what you're looking for:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MatcherDemo {

    private static final String REGEX = "[A-Z]+";
    private static final String INPUT = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]";

    public static void main(String[] args) {
       Pattern p = Pattern.compile(REGEX);
       //  get a matcher object
       Matcher m = p.matcher(INPUT);
       List<String> sequences = new Vector<String>();
       while(m.find()) {
           sequences.add(INPUT.substring(m.start(), m.end()));
       }
    }
}

Upvotes: 0

Stephen Quan
Stephen Quan

Reputation: 26131

I think you should do a replace all regular expression to turn the character you don't want into a delimiter, perhaps something like this:

  1. str.replaceAll("[^A-Z]+", " ")
  2. Trim any leading or trailing spaces.
  3. Then, if you wish, you can call str.split(" ")

Upvotes: 0

apines
apines

Reputation: 1262

If you want just to extract all the uppercase letter use [A-Z]+, if you want just uppercase substring, meaning that if you have lowercase letters you don't need it (HELLO is ok but Hello is not) then use \b[A-Z]+\b

Upvotes: 0

Nick Garvey
Nick Garvey

Reputation: 3000

This should demonstrate the proper syntax and method. More details can be found here http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html and http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html

String myStr = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]";
Pattern upperCase = Pattern.compile("[A-Z]+");
Matcher matcher = upperCase.matcher(myStr);
List<String> results = new ArrayList<String>();

while (matcher.find()) {
    results.add(matcher.group());
}

for (String s : results) {
    System.out.println(s);
}

The [A-Z]+ part is the regular expression which does most of the work. There are a lot of strong regular expression tutorials if you want to look more into it.

Upvotes: 5

Related Questions