0

I am parsing string and then I need to convert it to number. But if it contains division by 0 for example

String str1 = "1+2+3-5/0+4+6"
String str2 = "1+2+3-4/0.000 +4+6"

I must write in this string "Division by 0 error". My regex looks like this, but it is wrong.

String REGEXP_DIV_BY_0 = "(.*)([/0\\.0{1,4}](^[1-9]+))(.*)";

I can't create regular expression for this task, to match string if it contains division by 0.

byxor
  • 5,930
  • 4
  • 27
  • 44
  • 1
    What if somebody writes `2/(1-1)`? How will you detect that? – byxor Dec 13 '16 at 13:16
  • 8
    what about `x / (1 - 1)` ? RegEx is not the choice for this, why not evaluate it and look for an exception? – Alex K. Dec 13 '16 at 13:16
  • In my string i have already calculated those expression, so i have only numbers and operations without any scopes just like i wrote in my example – Maksym Vasylenko Dec 13 '16 at 13:20
  • Why calculate everything except the division? Just divide it and see if it fails. – byxor Dec 13 '16 at 13:21
  • To evaluate this expression (full string) i am using method eval() and give to him my string. But i want check it with regular expression before, if String contains (/0 or /0.000) and if it's true, not to execute method eval() – Maksym Vasylenko Dec 13 '16 at 13:25
  • "But i want check (...) if String contains (/0 or /0.000)" - so just use method `String.contains(CharSequence)` twice... – Jaroslaw Pawlak Dec 13 '16 at 13:59

4 Answers4

2

This should do the job: .*\/0([^.]|$|\.(0{4,}.*|0{1,4}([^0-9]|$))).* Here you can play around with it: https://regex101.com/r/lwAVan/1

Martin Cup
  • 2,399
  • 1
  • 21
  • 32
1

I'd do:

/0+(?:\.?0*)?(?!\d)

Explanation:

/       : a slash (escape it if necessary)
0+      : 1 or more 0
(?:     : non capture group
  \.?0* : an optional comma followed by 0 or more 0's
)?      : end group (optional)
(?!\d)  : negative lookahead, assume there're no more digit followed the 0's
Toto
  • 89,455
  • 62
  • 89
  • 125
  • 1
    This is the best answer here by far, simply for the explanation. Regex looks like a mess to me. – byxor Dec 13 '16 at 15:23
1

regex is not the right way to evaluate an expression, there are too many cases, like 1/0, 1/(1-1), 1/(5+5-10), 1/(2*2-2^2)....

you can go with ScriptEngineManager:

    String str1 = "555/0";      
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("js");
    try {
        System.out.println(engine.eval(str1));
    }
    catch (ScriptException e) {
        e.printStackTrace();
    }

If there is /0 case, the result would be "Infinity".

Kent
  • 189,393
  • 32
  • 233
  • 301
  • I am using this Script manager, but before i execute method eval, i want to check with regex if it string contains division by 0. I eval all those expression in scopes and calculated them earlier(don't estimate for logic, there is such task:)) and i have string which contains only numbers and operations without scopes, so there won't be situations like 1/(1-1), 1/(5+5-10), 1/(2*2-2^2) – Maksym Vasylenko Dec 13 '16 at 13:53
  • `sin() cos() sqrt() log() pow() PI ... ` all those are valid as well – Kent Dec 13 '16 at 14:43
0

It's a little strange, but if you really must do this, then something like the following pattern should work:

0\.0{1,4}\D|/0[^\d.]

Since your requirements aren't very clear, it's hard to say how tolerant it needs to be. For instance, this pattern doesn't allow for white-space between the division operator and the divisor. It also assumes that there will always be some text after the numbers, which as long long as it is always written as a java string literal, then that's a safe assumption.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105