正则表达式 - Java源方法识别 [英] Regular Expressions - Java source method identification

查看:107
本文介绍了正则表达式 - Java源方法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试识别Java源代码中的方法。一切进展顺利,但是这个问题让我的代码停止了,因为我的方法没有识别出类名。在任何情况下它都很乱,我首先确定它是一个方法,然后我运行第二个表达式来尝试提取类名,这是它出错的地方。这是我的具体细节:



例如:

I am trying to identify methods in Java source. All was going well, but this one brought my code to a halt because my method did not identify the class name. It was messy in any case, I firstly identified that it was a method, then I ran a second expression to try and extract the class name, which is where it went wrong. Here is my specifics:

example:

public double calculateAmount (int iCode, double dRate, double dAmount) {





规则:



1.它始终以 public

开头2.数据类型可能是任何内容(即 double 可能 int void 等)

3.它可能包含也可能不包含参数在括号之间

4.该行总是以{



结束我在上一个问题中得到了建议,将完整的来源视为一弦。出于多种原因,并且因为我已经进行逐行分析,我一次只能工作一行。



到目前为止我有这个但是还没能在Regex测试仪中解决它:



1.我可以识别 public

\ bpublic

2.然后我们有一个空格和另一个字:

\ bpublic \s + \ w + \s +

3.然后我们有我的方法名称 - 如何捕获它?

\ bpublic \s + \w + \ s + ~~~~~~

4.接下来我们有两个括号,可能介于两者之间的东西,但不一定是

\ bpublic \s + \ w + \ s + ~~~~~~~ \(。* \)

5.我们结束{:

\ bpublic \s + \w + \ s + ~~~~~~ \(。* \\ \\)\s + \ {





提前致谢!



Rules:

1. It will always start with public
2. The datatype may be anything (i.e the double may be int, void, etc)
3. It may or may not include parameters between the parentheses
4. The line will always end with {

I have had advice in a previous question to treat the full source as one string. For a number of reasons and because I am already a long way into line-by-line analysis, I am working one line at a time.

So far I have this but have not been able to resolve it in a Regex tester:

1. I can identify public :
\bpublic
2. Then we have a space and another word :
\bpublic\s+\w+\s+
3. Then we have my method name - how do I capture this?
\bpublic\s+\w+\s+ ~~~~~~
4. Next we have 2 parentheses, possibly with something in between, but not necessarily
\bpublic\s+\w+\s+ ~~~~~~ \(.*\)
5. We end with a { :
\bpublic\s+\w+\s+ ~~~~~~ \(.*\)\s+\{


Thanks in advance!

推荐答案

我在 上使用正则表达式生成器 www.txt2re.com [ ^ ]生成这个:



I used the Regular Expression Generator at www.txt2re.com[^] to generate this:

// URL that generated this code:
// http://txt2re.com/index-csharp.php3?s=public%20double%20calculateAmount%20(int%20iCode,%20double%20dRate,%20double%20dAmount)%20{&-9&21&6&22&2&23&-99&29&-161&-98&1

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string txt="public double calculateAmount (int iCode, double dRate, double dAmount) {";

      string re1="(public)";    // Word 1
      string re2="(\\s+)";  // White Space 1
      string re3="((?:[a-z][a-z]+))";   // Word 2
      string re4="(\\s+)";  // White Space 2
      string re5="((?:[a-z][a-z]+))";   // Word 3
      string re6="(\\s+)";  // White Space 3
      string re7="(\\(.*\\))";  // Round Braces 1
      string re8="(\\s+)";  // White Space 4
      string re9="(\\{)";   // Any Single Character 1

      Regex r = new Regex(re1+re2+re3+re4+re5+re6+re7+re8+re9,RegexOptions.IgnoreCase|RegexOptions.Singleline);
      Match m = r.Match(txt);
      if (m.Success)
      {
            String word1=m.Groups[1].ToString();
            String ws1=m.Groups[2].ToString();
            String word2=m.Groups[3].ToString();
            String ws2=m.Groups[4].ToString();
            String word3=m.Groups[5].ToString();
            String ws3=m.Groups[6].ToString();
            String rbraces1=m.Groups[7].ToString();
            String ws4=m.Groups[8].ToString();
            String c1=m.Groups[9].ToString();
            Console.Write("("+word1.ToString()+")"+"("+ws1.ToString()+")"+"("+word2.ToString()+")"+"("+ws2.ToString()+")"+"("+word3.ToString()+")"+"("+ws3.ToString()+")"+"("+rbraces1.ToString()+")"+"("+ws4.ToString()+")"+"("+c1.ToString()+")"+"\n");
      }
      Console.ReadLine();
    }
  }
}


这篇关于正则表达式 - Java源方法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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