有效:
((int)10)
(int)10
((char)((x+y)&1))
((int *)1)
无效:
(a-b)
(a&=b)
一个
((a))
最佳答案
(平衡)括号表达式的语言是非正则,即,您不能编写匹配此类字符串的正则表达式。
参见 SO question: Why are regular expressions called "regular" expressions和 Wikipedia: Regular Languages .
您需要使用更强大的解析技术,例如 CFG,例如 ANTLR .
您可以从以下内容开始:
CastedExpression ::= Cast Expression | LPAR CastedExpression RPAR
Cast ::= LPAR Type RPAR
Expression ::= Sum | Product | Litteral | LPAR Expression RPAR | ...
Type ::= char | int | Type ASTERISK | ...
(如果您发现任何明显的改进,请随时编辑上面的语法)。
关于java - 需要正则表达式 : It should match for following patterns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3903965/