views:

59

answers:

2

I have the following pattern:

Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC) 

I want the final output to be:

String[] one = {"Jan", "Feb", "Mar"};
String[] two = {"COMPANY","ASP","INC"};

Please help. Anyone!!?

+4  A: 

A complete example that stores the results in String[] one and String[] two would look like this:

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {

        String str = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

        ArrayList<String> monthArr = new ArrayList<String>();
        ArrayList<String> dataArr  = new ArrayList<String>();

        // Part 1: \\b(\\p{Alpha}+): Word boundary, one or more characters
        // Part 2: \\(([^)]+)\\): "(", anything but ")" one or more times, ")" 
        Pattern p = Pattern.compile("\\b(\\p{Alpha}+)\\(([^)]+)\\)");
        Matcher m = p.matcher(str);

        while (m.find()) {
            monthArr.add(m.group(1));  // m.group(1) = "Jan", "Feb" and so on
            dataArr.add(m.group(2));   // m.group(2) = "COMPANY, "ASP", and so on
        }

        String[] one = monthArr.toArray(new String[0]);
        String[] two = dataArr.toArray(new String[0]);
    }
}
aioobe
This does not use the Java String API, as requested by the OP.
Erick Robertson
I'd say that Javas regular expression API is a part of the API for strings. Depends on how you interpret the question though. (btw, is ArrayList part of the string api? ;)
aioobe
Nope, but I used the String API in my answer. I do not consider the `java.util.regex` package as part of the Java String API. If the subject had been "To derive specific data from a string using regex", I would be on board with your answer.
Erick Robertson
Thanks. Can u temme how this works..."([A-Z][a-z]{2})\\(([^)]+)\\)"
Thinking
especially {2} part?
Thinking
Oh, it's just a capital letter `[A-Z]`, followed by 2 `{2}` lower case letters. You could also have for instance `[A-Za-z]{3}` if you like three arbitrarily cased letters. Or, if it's only months, you could do `Jan|Feb|Mar|...|Dec`.
aioobe
And what if i'm not sure of the number of letters?
Thinking
Then I'd use `p = Pattern.compile("\\b(\\p{Alpha}+)\\(([^)]+)\\)")` which says, "word-boundary", "one or more alphabetical characters", followed by "(" ... Updated the answer.
aioobe
If you want to include digits, change alpha, to alnum: `p = Pattern.compile("\\b(\\p{Alnum}+)\(([^)]+)\)")`
aioobe
Thanks..., its working fine:)
Thinking
Then you should accept the answer by clicking the checkbox ;)
aioobe
ohhh...sure, i will.
Thinking
i was working on another possibility
Thinking
if str = "Jan(COMPANY) the format of the o/p has to be same even with the extra paranthesis...(Feb(ASP))
Thinking
working.....thanks again:)
Thinking
sorry to bother....one more issue. can u help
Thinking
sure. But I recommend you to ask a new question. This "comment" thread has sort of out-grown the question.
aioobe
suppose the string contains a term like rain-drop. only drop is logged.
Thinking
I removed my DV from your answer, and replaced it with an upvote.
Erick Robertson
A: 

Iterate across the characters in the string and store what you need.

String s = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

ArrayList<String> monthsList = new ArrayList<String>();
ArrayList<String> companyList = new ArrayList<String>();

StringBuilder b = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);

    switch (c) {

    case ' ':
        b.setLength(0);
        break;
    case '(':
        monthsList.add(b.toString());
        b.setLength(0);
        break;
    case ')':
        companyList.add(b.toString());
        b.setLength(0);
        break;
    default:
        b.append(c);
        break;
    }
}

String[] monthArray = monthsList.toArray(new String[] {});
String[] companyArray = companyList.toArray(new String[] {});
dogbane