Issue with regex expression parameter supplied to String split method.
I created a small test program below to to test the String split function. Its not working as I would expect it to work.
I expect the program to print:
Parts of the myString = #if
Parts of the myString = ABC
Parts of the myString= CCC
Parts of the myString = DDD
However, the program generates:
Parts of the myString = #if
Parts of the myString = ABC
Parts of the myString =
Parts of the myString =
Parts of the myString =
Parts of the myString = CCC
Parts of the myString =
Parts of the myString =
Parts of the myString =
Parts of the myString = DDD
Also, if I replace '#if ABC && CCC || DDD" with #if ABC & CCC | DDD", the program still generates the same results.
Furthermore, I realize | is a special character, but I get an compile time error if I change it to \|{2}.
Could someone please enlighten me. Thanks, Stephen
Code:
public class TestRegex {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String regexString = "[()!&{2}|{2} ]" ;
String myString = "#if ABC && CCC || DDD";
String[] myStringArray = myString.split(regexString);
for (String parts : myStringArray)
{
System.out.println("Parts of the myString = " + parts);
}
}
}
Re: Issue with regex expression parameter supplied to String split method.
Your regex splits on every char contained in your square brackets, so it should be no surprise that you're getting the result you see. Why not instead use a String that holds variable amount of whitespace in front and behind, and then in parenthesis has a choice of space, or && or || (the latter escaped)? When I do this, my result is:
Parts of the myString = #if
Parts of the myString = ABC
Parts of the myString = CCC
Parts of the myString = DDD
Re: Issue with regex expression parameter supplied to String split method.
For example...
Code:
String regex2 = "\\s*( |&{1,2}|\\|{1,2})\\s*";
Re: Issue with regex expression parameter supplied to String split method.
Do you mean something like this?: String regexString = "[\\s*( &&\\|\\|)\\s*]"
For that expression my program returns:
Parts of the myString = #if
Parts of the myString = ABC
Parts of the myString = &&
Parts of the myString = CCC
Parts of the myString = ||
Parts of the myString = DDD
If I use String regexString = "[\\s*( &{2}\\|\\|)\\s*]", I get the same result as before.
Also, using a single escape character before | (e.g. \|) shows up as an error.
Re: Issue with regex expression parameter supplied to String split method.
I just noticed you posted the regex, so you can disregard post #4.
Re: Issue with regex expression parameter supplied to String split method.
Your problem is that you're using the square brackets where you shouldn't. Those are for extracting a single character only when you want a complete String.
Re: Issue with regex expression parameter supplied to String split method.
Fantastic! That works. Thanks.
Re: Issue with regex expression parameter supplied to String split method.