0

I am using the following regular expression:

if (input.matches("^[\\d\\s()\bAND\b\bOR\b.]+$")) {

   // do something

}

My goal is to validate only numbers (int and decimal), spaces, ( ), dot, AND, OR. Everything else is not allowed. It seems like it works but I'm having problems with word boundary \bAND\b and \bOR\b Eg. I can't not enter ANDWE or EEE or any combination, but what I can do is AN or A. How do I only allow two words AND or OR in a string? They are optional in a string.

Input examples

0.10 AND 23 - valid

12 AND (15 OR 0.2) - valid

12 OR 190 - valid

12 AND AND 12 - valid

12 A 233 - invalis

antohoho
  • 950
  • 2
  • 17
  • 37
  • 7
    You can not use words inside of a character class `[]`. – hwnd Apr 22 '14 at 21:39
  • 1
    How does your input look like? Give some examples! I meen does it have to be valid mathematic expressions or does any sequence f.e. "AND AND OR 333435553" qualify – Sara S Apr 22 '14 at 21:42
  • no, it should not be valid mathematical, only AND and OR accepted words – antohoho Apr 22 '14 at 21:46
  • Okay. I assume that you have to validate the expression also? Perhaps you could make an easy Parser-class that returns error if an incorrect character appears? You will anyhow need to check that the parenthesis are matching etc... – Sara S Apr 22 '14 at 21:51
  • Check this out for Parsing and evaluating expressions: http://stackoverflow.com/questions/13421424/how-to-evaluate-an-infix-expression-in-just-one-scan-using-stacks – Sara S Apr 22 '14 at 21:53

2 Answers2

1

You want numbers, spaces, parenthesis, dot and AND, and OR.

Here is one way to do it, by specifying the single characters inside the character class, and separating the two words using the OR operator |. Note that \b has the \ escaped, because \b is itself an string escape sequence.

if (input.matches("^([\\d\\s().]|\\bAND\\b|\\bOR\\b)+$")) {

   // do something

}

Here is a test program on your input.

import java.util.regex.*;


public class Main{
    public static void main(String[] args) {
        String[] myArray = new String[] {"0.10 AND 23","12 AND (15 OR 0.2)","12 OR 190","12 AND AND 12","12 A 233"};
        String regex = "^([\\d\\s().]|\\bAND\\b|\\bOR\\b)+$";

        for (int i = 0; i < myArray.length; i++)
        System.out.println(myArray[i].matches(regex));

    }
}

Output:

true
true
true
true
false
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

You can't parse a context free language using one regular expression.

The simplest cast is parenthesis balancing. You can't detect if an string consisting of just parenthesis is correctly balanced. For example, "()(()())". There is no way to detect if this string is correct using just one regular expression.

There are extensions to regular expression engines where you can use an internal stack. Check this answer on SO.

Community
  • 1
  • 1
vz0
  • 32,345
  • 7
  • 44
  • 77