0

I have a string like : String test = "A1=CA2=BOA2=RA4=OA11=O";
Now I want output like that : A1,A2,A2,A4,A11

If my string like :

String test = "[HEADER_1] [DATA] \n A10=T,(\"Re-N RO-25 M-N P-N (B)\"),1.0:0.8,R=25.0 F-7.829215,-4.032765 A20=B,R2,M=XY,R=29.999999999999996 F564.997550,669.454680";


public static void main(String[] args) {
    String test = "A1=CA2=BOA2=RA4=O";
    String data[] = test.split("[A-a]\\d{1,100}=");

    for (String str : data) {
      System.out.println("Split data:"+str);
    }      
}

What would be the regex for that?

2
  • What's the problem with the code that you have already written? Commented Feb 5, 2016 at 5:54
  • your output should contain only start from A or other letters will be there lik B1,b2, BB1,AA1 Commented Feb 5, 2016 at 6:24

3 Answers 3

2

I would not use split for that. The regex for the parts that you want is /a\d+/i. If you use this and the Solution from Create array of regex matches to collect all matches in a list, you can easily create the desired output.

Converted the Regex in Java-Parlor:

public static void main(String[] args) {
    String test = "A1=CA2=BOA2=RA4=OA11=O";
    List<String> allMatches = new ArrayList<String>();
    Matcher m = Pattern.compile("(a\\d+)", Pattern.CASE_INSENSITIVE).matcher(test);

    while (m.find()) {
       allMatches.add(m.group());
    }
}

allMatches now contains A1 A2 A2 A4 A11

Sign up to request clarification or add additional context in comments.

7 Comments

I didn't vote you down, but I would not attempt to use a single regex here. The reason is that regex does not work so well in the case of an unbounded number of matches, which is precisely what the OP has.
Could you explain which cases you mean?
Your regex won't work, just try using it. You'd be better off going with String.split() first and then matching against each part.
If you are sure, then post a working snippet of Java code so that the OP may test it.
I added a sample program
|
0

The regex you are probably looking for is [A-a][0-9]{1,2}.

So in Java,

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


String test = "A1=CA2=BOA2=RA4=OA11=O";
Matcher m = Pattern.compile("[A-a][0-9]{1,2}").matcher(test);
while (m.find()) {
    System.out.print(m.group(0)+",");
}

1 Comment

@RahulBhawar. You are most definitely right! But you didn't bother to read the question to see that that isn't a use case. Furthermore, the OPs broken regex has the intent to match numbers 1-100.
0
String test = "A1=CA2=BOA2=RA4=OA11=O";
String[] parts = test.split("=");
String[] output = new String[parts.length];
for (int i=0; i < parts.length; ++i) {
    output[i] = parts[i].replaceAll("^.*(A\\d*)$", "$1");
    System.out.println(parts[i] + " becomes " + output[i]);
}

Output:

A1 becomes A1
CA2 becomes A2
BOA2 becomes A2
RA4 becomes A4
OA11 becomes A11
O becomes O

4 Comments

"OA11 becomes OA11" - Shouldn't OA11 go to A11?
@intboolstring Good call. You posted so quickly, I had no time for thorough testing ^ ^
I can not split using '=' because my string contains multiple times '='. For example "A1=C,R=25A2=BOA2=RA4=OA11=O";
@ShiladittyaChakraborty I think you are confused on how String.split() works. It will split up the string using all = signs, no matter how many appear. Please test my code to see for yourself. You should be able to just cut and paste it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.