Hello,
I have a string which contains letters, numbers, and symbols. I'd like to check the string to make sure it doesn't contain any symbols. One solution is to pass it through a for loop and use the charAt method. I don't want to do that. I'd like to use regular expressions!
This example, although good for comparing numbers and letters, it's not good when comparing symbols. Now when I say symbols, I'm referring to every character on your keyboard:Code:String input = "I have a cat, but I like my dog better.";
Pattern p = Pattern.compile("(mouse|cat|dog|wolf|bear|human)");
Matcher m = p.matcher(input);
while (m.find())
System.out.println("Found a " + m.group() + ".");
`~!@#$%^&*()-_=+[{]};:'"\|/?.>,<
If I replace this Pattern p = Pattern.compile("(mouse|cat|dog|wolf|bear|human)") ; with this Pattern p = Pattern.compile("(%|&"); and run the app, IT WILL ALWAYS PRINT FOUND. Why is that? Is their a fix around it? and it HAS TO BE the way I need it to be. The symbols must be in the regex. I'm aware that there is an alphanumeric method that I can use which simplifies things for me but I need it to be this way. I might decide to use a different language for strings. I might even want to include subscript numbers for example. I'm aware that I'll need to add utf-8 as a parameter to the bufferreader when i create its object.
So is there a solution? Or do I have to use the charAt method? It seems so "old" compared to regex >.<
Thank you.
EDIT: what im trying to say is that if i put numbers or letters between (" ") in the Pattern line, java accepts it. But if I put symbols in there, java won't recognize it at all. Wat do i do?
EDIT EDIT: I think i fixed it. I think I need to add \\ to most of these symbols as they have a meaning in regular expressions. Like ^ means ANYTHING OTHER THAN what comes after it so i'll need to add \\ to ^. Will keep you updated. But if this isn't the solution, or if there's a better solution, feel free to answer :D

