开发者

Regular Expression to Match Sequence of Characters

I need a Regular Expression to match a sequence of characters that make up a query. An example of my definition of a query is like this : and(cat,dog) In a more general definition, it can be represented as operator(operand, operan开发者_运维技巧d) It is made up of an operator that contains 2 operands. The operands can be any single ALPHABETIC word(non-numeric) or even another subquery. and an example is and(goat,or(zebra,bear)) The operators are AND, OR, NOT and a query can get more and more AMBIGUOUS to an extent as far as possible. There is no WHITESPACE within the query and I need the regex to use in a Java application. Note: A NOT operator can have only one operand e.g. not(tiger). Thanks in advance.


The subquery as an operand makes this impossible. Regular expressions aren't powerful enough for nested stuff, you'll need to use context-free grammars.


Your problematic may be much more complicated and it is hard to guess your exact direction. However, this simple Java example maybe a good start point for you:


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test { 

  public static void main(String[] args) {
    String s = "add(1,sub(2,3))";
    Pattern p = Pattern.compile("(.*?)\\((.*)\\)");
    Matcher m1 = p.matcher(s);
    if (m1.matches()) {
      System.out.println(m1.group(1));
      System.out.println(m1.group(2));
    }
    System.out.println();
    Matcher m2 = p.matcher(m1.group(2));
    if (m2.matches()) {
      System.out.println(m2.group(1));
      System.out.println(m2.group(2));
    }
  }
}

Produces output like:


add
1,sub(2,3)

1,sub
2,3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜