Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) {


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!
Posted
Updated 23-Feb-13 2:53am
v2

1 solution

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

C#
// 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();
    }
  }
}
 
Share this answer
 
Comments
Steve Vink 1-Mar-13 4:20am    
Thanks Mike. I really struggled to understand the tool, but I have learned lots about regular expressions by dissecting it.
I now understand that the parts in parentheses become groups, and that by removing the parentheses it doesn't retain the details of the match.

There was one change I had to make. The [a-z] needed to be [A-z], as there is the possibility of uppercase in the method names.

Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900