doupu1727 2014-09-16 17:31
浏览 73

PHP:RegEx用于解析JAVA类中的特定函数

I have to parse JAVA classes with PHP. All java classes implement my interface, so there is always corresponding function required by me in the class. It looks like:

public void init(Something driver, Parameters params) { ... }

I came up with the following regular expression:

/public\s+void\s+init\s*\(\s*Something\s+(\w+)\s*,\s*Parameters\s+(\w+)\s*\)\s*\{([^\}]+)\}\s*/

It works fine except the situations when there is "}" symbol somewhere inside the body of my function. Is there any way to handle these situations?

  • 写回答

1条回答

  • drwo32555 2014-09-16 17:40
    关注

    Give a try to the below regex,

    public\s+void\s+init\s*\(\s*Something\s+(\w+)\s*,\s*Parameters\s+(\w+)\s*\)\s*\{((?:[^{}]*(?:{[^{}]*})?)+[^{}]*)\}\s*
    

    DEMO

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计