Search code examples
javaregexstringregular-languagedivide-by-zero

Regular expression for division by 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.


Solution

  • 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