0

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.

Brayden
  • 16
  • 1
  • 1
  • 3
  • Please share some examples from what sould be matched, since you did make everything, but the one `.` optional which matches anything it is not really clear. – Leonard Brünings Oct 20 '17 at 00:34
  • Just "say" in your RegEx that it must start with the minimum representation for a term (and maybe its combinations) – x80486 Oct 20 '17 at 00:34
  • Sometimes the RegEx became too complex... Shouldn't be easy to verify if it's an empty String? – MiguelKVidal Oct 20 '17 at 00:37
  • if {termFormat.length > 0} ? – miknik Oct 20 '17 at 00:38
  • your wording in the "examples" is somewhat confusing, what example doesn't behave as expected? – Daniel Oct 20 '17 at 01:47
  • @ɐuıɥɔɐɯ I tried something similar to that but for whatever reason it wouldn't work, it was pretty obvious it should've but after an hour of messing with it I gave up. – Brayden Oct 20 '17 at 03:40
  • @LeonardBrünings do those help at all? And the period is just for the decimal that would be in the term – Brayden Oct 20 '17 at 03:41
  • @miknik that works for the first part where I need it, but I edited and added a second part where that wouldn't work. – Brayden Oct 20 '17 at 03:42
  • @MiguelKVidal same thing, read above comment. – Brayden Oct 20 '17 at 03:43
  • 1
    @MondKin just modified the wording, is it more clear now? – Brayden Oct 20 '17 at 03:43
  • 1
    After many more tries and wasted hours you will eventually find out that regular expressions are not the right tool for this. At the latest this will happen when you allow subexpressions in parenthesis. – Henry Oct 20 '17 at 05:01
  • See [Boann's answer](https://stackoverflow.com/a/26227947/3832970) – Wiktor Stribiżew Oct 20 '17 at 09:33

0 Answers0