正则表达式:它应匹配以下模式 [英] Regex required: It should match for following patterns
本文介绍了正则表达式:它应匹配以下模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有效期:
-
((int)10)
-
(int)10
-
((char)((x + y )& 1))
-
((int *)1)
((int)10)
(int)10
((char)((x+y)&1))
((int *)1)
无效:
-
(ab)
-
(a& = b)
-
a
-
((a))
(a-b)
(a&=b)
a
((a))
推荐答案
(平衡的)带括号的表达式的语言是不常规,即你不能写匹配这些字符串的正则表达式。
The language of (balanced) parenthesized expressions is not regular, i.e., you can't write a regular expressions matching these kind of strings.
参见 SO问题:为什么正则表达式称为常规表达式 和维基百科:常规语言。
您需要使用功能更强大的解析技术ique,例如CFG,例如 ANTLR 。
You need to work with a more capable parsing technique such as a CFG with for instance ANTLR.
您可以从以下内容开始:
You could start with something like:
CastedExpression ::= Cast Expression | LPAR CastedExpression RPAR
Cast ::= LPAR Type RPAR
Expression ::= Sum | Product | Litteral | LPAR Expression RPAR | ...
Type ::= char | int | Type ASTERISK | ...
(如果你发现任何明显的改进,可以随意编辑上面的语法)。
这篇关于正则表达式:它应匹配以下模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文