正则表达式:它应匹配以下模式 [英] Regex required: It should match for following patterns

查看:115
本文介绍了正则表达式:它应匹配以下模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有效期:


  1. ((int)10)

  2. (int)10

  3. ((char)((x + y )& 1))

  4. ((int *)1)

  1. ((int)10)
  2. (int)10
  3. ((char)((x+y)&1))
  4. ((int *)1)

无效:


  1. (ab)

  2. (a& = b)

  3. a

  4. ((a))

  1. (a-b)
  2. (a&=b)
  3. a
  4. ((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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆