Home » Java » Java regex that handles several possibilites

Java regex that handles several possibilites

Posted by: admin October 21, 2018 Leave a comment


I am trying to find a regex for the following user generated possibilities:

  • ÷2%3%x#4%2$@


  • ÷2%x#4%2$@


  • ÷2%x#4$@


  • ÷2%x@

To understand the expression, it is a fraction whose numerator lies between
the ÷ and the first %, and the denominator lies from first % to the @.
But, the denominator has an exponent, which lies from the # to $.

The user can input whatever number he/she desires, but the structure stays the same. Notice that the number can also be a decimal.

The structure is as follows: ÷(a number, if its two or more digits a % will be in between the digits)x(a group that consists of a number(s), also the symbols # , $ and a %(s) which can also alternate between the digits)@

Remember, the number can be a decimal number.

I am trying to use the following regex with no success:


I think that the group (-?\d+(\.\d*)?){0,1} is complicating things up.
Also, I have not accounted for the % within this group which could occur.

any suggestions, thank you