So I have a regex to see if user input is an algebraic term or not (mostly). It works the way I want to totally fine, except for the fact that if you put nothing or white space in, it will say it is a term because each of the expressions can happen 0 times. I've tried to find workarounds, some lengthy, but couldn't find any.
class termChecker {
Pattern termFormat = Pattern.compile("-?\\d*.\\d*\\w?");
Matcher termMatcher;
boolean termMatches;
public termChecker(String term){
termMatcher = termFormat.matcher(term.replaceAll("\\s", ""));
termMatches = termMatcher.matches();
}
}
All I need is a solution, preferably short. (If you test this and it already does what I asked, I need this for a few other bits so that is why)
Edit: yeah this needs a bit more context. As well as here, I need a regex that will also work here:
class equationChecker{
char[] operations = "+-*/^".toCharArray();
int termNum;
Pattern equationFormat = Pattern.compile("((-?\\d*.\\d*\\w?)|((-?\\d|.\\d|\\w)+){1}(" + Pattern.quote("+") + "|-|(/)|((^)(-?\\d*.\\d*\\w?)?)|){1})*(-?\\d*.\\d*\\w?){1}");
Matcher equationMatcher;
boolean equationMatches;
public equationChecker(String equation){
equationMatcher = equationFormat.matcher(equation.replace("(", "").replace(")", "").replaceAll("\\s", ""));
equationMatches = equationMatcher.matches();
String[][] equationParts = new String[2][termNum];
}
}
So where ever (-?\\d*.\\d*\\w?)|((-?\\d|.\\d|\\w)+
is is essentially where I need the new regex to work.
Examples: -245.63x = true, 14 = true, x = true, .719 = true, "word" = false. What is giving me trouble is "" or " " should equal false, but they don't. Hope that helps a bit.