2

I have got a program that takes user input in a form of logical expression (for example: (p=>(r||q)) ) and it divides the string in to substrings which are defined by brackets. I am using pattern and matcher.
So, for example user inputs:
((p||q)=>r).
I want to get 2 substrings which are:
p||q
and (p||q)=>r.
However I am only getting this:
(p||q

Here is the code that I am using

 Scanner scanner = new Scanner(System.in);
 System.out.println("Enter formula: ");
 String formula = scanner.next();
 Pattern pattern = Pattern.compile("\\((.*?)\\)");
 Matcher matcher = pattern.matcher(formula);
         while(matcher.find())
         {
          String s = matcher.group(1);

          System.out.println(s);
          }

So I need a way that the program finds all the substrings in that string.

1
  • Regular expressions are poorly suited to this task. I suggest you do the parsing manually or make use of a parsing library. Commented Dec 21, 2011 at 18:15

2 Answers 2

2

You can't do this with regular expressions.

Because the usage of brackets is a sign that what you want to do goes beyond the capabilities of regular expressions. Regular expressions describe a Chomsky-3 grammar which can't have bracket structures. Bracket structures are available in Chomsky-2 grammars. So you have to define a corresponding grammar with parsing rules. A good library that might help you achieving what you want is ANTLR.

Sign up to request clarification or add additional context in comments.

Comments

2

As I said in my comment, regular expressions do not handle nested brackets well at all. However, parsing them manually is extraordinarily simple if you have a stack. Here is some sample code:

public static void main(String[] args) throws InterruptedException {
    findSubExpressions("((p||q)=>r)");
}

private static void findSubExpressions(String input) {
    Deque<Integer> startingBrackets = new LinkedList<Integer>();

    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '(') {
            startingBrackets.push(i);
        } else if (c == ')') {
            int correspondingStart = startingBrackets.pop();
            logSubExpression(input.substring(correspondingStart+1, i));
        }
    }
}

private static void logSubExpression(String subExpression) {
    System.out.println(subExpression);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.