Ok, before I explain right away, I want to mention that the purpose of this program I'm making is to create a class which contains general use methods that we use in projects for my Computer Science class. Anyway, the purpose behind this particular method I'm having trouble with is to take the input String (like a line of text the user enters) and it will replace all non-letter characters (~!@#$%^&* and such) with a new String that the user or programmer chooses.
For example: The user is asked to enter a line of text with symbols included in it (!~@#$%^&*, etc.) and then chooses what the replace every character, the non-letter ones, with. So if they want to replace them with a blank space it would do this:
Input: This*^ is a&test!@sente~nce
Output: This is a test sente nce
Or if they want something other than a blank space, like an underscore:
Input: This*^ is a&test!@sente~nce
Output: This___is_a _test__sente_nce
Now, my program works fine up to this point. However, when I get to the point where I want to change a [ into, for example, a blank space, I get this error:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^ at java.util.regex.Pattern.error(Pattern.java:1713) at java.util.regex.Pattern.clazz(Pattern.java:2254) at java.util.regex.Pattern.sequence(Pattern.java:1818) at java.util.regex.Pattern.expr(Pattern.java:1752) at java.util.regex.Pattern.compile(Pattern.java:1460) at java.util.regex.Pattern.<init>(Pattern.java:1133) at java.util.regex.Pattern.compile(Pattern.java:823) at java.lang.String.replaceAll(String.java:2189) at CommonUse.replaceChars(CommonUse.java:68) at Test.main(Test.java:9)
CommonUse is the name of the class which contains replaceChars(), obviously, and the Test class is just that, a class to test my method.
So, at line 68 in my CommonUse class:
input = input.replaceAll("[", replacement);
I have looked around in the Java API on the regex (regular-expression) constructs and think this has something to do with the Groups and Capturing since the [ opens but never closes. I've tried messing around with escape characters like \\[ but that doesn't help any. And after reading on the Groups and Capturing, I tried all kind of combos using the ?: with no luck. I don't fully understand how regex works, other than the basic escape sequences for outputs.
So, can anyone help me figure out how to change any [ into another character/string? Also, I believe I'll have the same problem with characters like ], {, }, <, >, etc., so will one method apply to all of them?
Just for reference, here are some bits of code:
CommonUse class:
// Returns a new String of the parameter (input) after replacing all non-letter characters // ( ~!@#$%^&*()_+`-=[]\{}|;':",./<>?) with the string/character of choice defined in the second // parameter (replacement). public static String replaceChars(String input, String replacement) { input = input.replaceAll("0", replacement); input = input.replaceAll("1", replacement); input = input.replaceAll("2", replacement); input = input.replaceAll("3", replacement); input = input.replaceAll("4", replacement); input = input.replaceAll("5", replacement); input = input.replaceAll("6", replacement); input = input.replaceAll("7", replacement); input = input.replaceAll("8", replacement); input = input.replaceAll("9", replacement); input = input.replaceAll(" ", replacement); input = input.replaceAll("~", replacement); input = input.replaceAll("!", replacement); input = input.replaceAll("@", replacement); input = input.replaceAll("#", replacement); input = input.replaceAll("\\$", replacement); input = input.replaceAll("%", replacement); input = input.replaceAll("\\^", replacement); input = input.replaceAll("&", replacement); input = input.replaceAll("\\*", replacement); input = input.replaceAll("\\(", replacement); input = input.replaceAll("\\)", replacement); input = input.replaceAll("_", replacement); input = input.replaceAll("\\+", replacement); input = input.replaceAll("\\`", replacement); input = input.replaceAll("\\-", replacement); input = input.replaceAll("\\=", replacement); input = input.replaceAll("[", replacement); /* Commented out because I want to debug the [ problem first input = input.replaceAll("]", replacement); input = input.replaceAll("\\", replacement); input = input.replaceAll("{", replacement); input = input.replaceAll("}", replacement); input = input.replaceAll("\\|", replacement); input = input.replaceAll(";", replacement); input = input.replaceAll("'", replacement); input = input.replaceAll(":", replacement); input = input.replaceAll("\"", replacement); input = input.replaceAll(",", replacement); input = input.replaceAll(".", replacement); input = input.replaceAll("/", replacement); input = input.replaceAll("<", replacement); input = input.replaceAll(">", replacement); input = input.replaceAll("\\?", replacement); */ return input; }
Test class:
public class Test { public static void main(String[] args) { // Working with my own test string before trying user input String example = "this is a test ~!@#$%^& of the *()_+ pro`-=[ gram"; System.out.println(example); example = CommonUse.replaceChars(example, " "); System.out.println(example); } }
Any help is appreciated! Thanks!
EDIT: I managed to get it to work if I use an escape sequence for the unicode of [:
input = input.replaceAll("\\u005B", replacement);
So I suppose I just need to find the Unicode for all of those characters I have problems with? Is there a better way? And I'd really appreciate it if someone could explain to me WHY "\\u005B" works but "\\[" doesn't.
Thanks again!