Reputation: 11
I've looked through most of the regex questions here, and none match my particular case.
Say I have a string: "ABe(CD)(EF)GHi"
I want: "A", "Be", "(CD)", "(EF)", "G", "Hi"
I have tried:
.split("(?=[A-Z\\(\\)])"), which gives me: "A", "Be", "(", "C", "D", ")", "(", "E", "F", ")", "G", "Hi".
Any ideas?
Upvotes: 1
Views: 275
Reputation: 174706
Do matching instead of splitting.
String s = "ABe(CD)(EF)GHi";
Pattern regex = Pattern.compile("\([^()]*\)|[A-Z][a-z]+|[A-Z]");
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(0));
}
Upvotes: 0
Reputation: 21
Try this..
String array = "ABe(CD)(EF)GHi";
int i = 0;
for(int j=0; j<array.length();)
{
if(Character.isUpperCase(array.charAt(i)))
{
j++;
System.out.println(array.substring(i, i+1));
if(Character.isUpperCase(array.charAt(i+1)))
{ System.out.println(array.substring(i+1, i+3));
i = i+3;
j = j + 3;
}
}
else
{
System.out.println(array.substring(i+1, i+3));
i = i+4;
j = j + 3;
}
}
Upvotes: 0
Reputation: 159106
Try this:
String input = "ABe(CD)(EF)GHi";
String[] split = input.split("(?=[A-Z](?![^(]*\\)))|(?=\\()|(?<=\\))");
System.out.println(Arrays.toString(split));
Output
[A, Be, (CD), (EF), G, Hi]
Explained
(?= Before:
[A-Z] Uppercase letter
(?![^(]*\)) not followed by ')' without first seeing a '('
i.e. not between '(' and ')'
)
|(?= or before:
\( '('
)
|(?<= or after:
\) ')'
)
Upvotes: 5