2

I have some string patterns. Each pattern consist of two characters "A" and "B". My patterns are like "AA" or "ABA" or "AABBABA" or ...

I want to split these patterns and the output for these examples must be like: {"AA"} or {"A","B","A"} or {"AA","BB","A","B","A"}

What I tried so far :

String pattern = "AABBABA" //or whatever   
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split(firstChar);
for (String string : split) {
    Log.i("findPattern", "Splitted Pattern: " + string + "");
}

The problem with my code is that it removes all strings that are equal to firstChar.

What regular expression should I use to split my patterns to separated strings?

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78

3 Answers3

3

The idea behind this is, (.)\\1+ helps to match any number of repeated characters at very first and this |. helps to match all the other single characters. Finally put all the matched characters into a list and then print it.

   String s = "AABBABA";
   ArrayList<String> fields = new ArrayList<String>();


        Pattern regex = Pattern.compile("(.)\\1+|.");
        Matcher m = regex.matcher(s);
        while(m.find()){

           fields.add(m.group(0));

    }
    System.out.println(fields);
}

Output:

[AA, BB, A, B, A]

By defining all the above input inside an array.

   String s[] = {"AA", "ABA", "AABBABA"};
   Pattern regex = Pattern.compile("(.)\\1+|.");
   for(String i:s)
   {
       ArrayList<String> fields = new ArrayList<String>();
        Matcher m = regex.matcher(i);
        while(m.find()){

           fields.add(m.group(0));


    }
        System.out.println(fields);
}

Output:

[AA]
[A, B, A]
[AA, BB, A, B, A]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

I tried to follow your logics and got this code, perhaps, not what you want:

String pattern = "AABBABA"; //or whatever   
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split("(?!" + firstChar + ")");
for (String strng : split) 
{
   System.console().writer().println(strng);
}

Output:

AA                                                                                                                                  
B                                                                                                                                   
BA                                                                                                                                  
BA  

Or try the Matcher:

// String to be scanned to find the pattern.
String line = "AABBABA";
String pattern1 = "(A+|B+)";
Pattern r = Pattern.compile(pattern1);
Matcher m = r.matcher(line);
int count = 0;
while(m.find()) 
{
   count++;
   System.console().writer().println(m.group(0));
} 

Output:

AA                                                                                                                                                                  
BB                                                                                                                                                                  
A                                                                                                                                                                   
B                                                                                                                                                                   
A 
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

you may capture what you want instead of split using this pattern

(A+|B+)

Demo

alpha bravo
  • 7,838
  • 1
  • 19
  • 23