Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Parsing source code and matching methods with regex

Parsing source code and matching methods with regex

Scheduled Pinned Locked Moved C#
csharpregexjsonhelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Genbox
    wrote on last edited by
    #1

    Hi. I have some problems parsing C# source code with regular expressions and counting the available methods. this is what i use as test data: public int GetNumber(int number) { return number; } private string GetCountry() { return "England"; } public Constructor() { } private Constructor(string test) { } But every try I make at parsing this, both the methods and constructors gets matched as methods. The problem is that the visibility (public, private) of the method does not have to be there. example: void HelloWorld() Can anyone shed some light on this?

    I L 2 Replies Last reply
    0
    • G Genbox

      Hi. I have some problems parsing C# source code with regular expressions and counting the available methods. this is what i use as test data: public int GetNumber(int number) { return number; } private string GetCountry() { return "England"; } public Constructor() { } private Constructor(string test) { } But every try I make at parsing this, both the methods and constructors gets matched as methods. The problem is that the visibility (public, private) of the method does not have to be there. example: void HelloWorld() Can anyone shed some light on this?

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      If you only need to count the methods you could match both and then find which is the constructor and remove it. If you need more info than that regular expressions might limit you. If you compile it you could use reflection or you could use a complete parser like NRefactory (part of SharpDevelop).

      1 Reply Last reply
      0
      • G Genbox

        Hi. I have some problems parsing C# source code with regular expressions and counting the available methods. this is what i use as test data: public int GetNumber(int number) { return number; } private string GetCountry() { return "England"; } public Constructor() { } private Constructor(string test) { } But every try I make at parsing this, both the methods and constructors gets matched as methods. The problem is that the visibility (public, private) of the method does not have to be there. example: void HelloWorld() Can anyone shed some light on this?

        L Offline
        L Offline
        Leslie Sanford
        wrote on last edited by
        #3

        GentooBoxX wrote:

        But every try I make at parsing this, both the methods and constructors gets matched as methods. The problem is that the visibility (public, private) of the method does not have to be there. example: void HelloWorld()

        I've not had much experience with regex, unfortunately, so I can't suggest a regex expression that will work. However, as far as getting false positives with constructures, one way to eliminate them when you find a method is check the method's name. If the name matches that of the class, you can tag it as a constructor. As far as finding methods in the first place, hmm... Wouldn't every line that ends with a right closing parenthesis ')' and is not followed by a semicolone indicate that the line is the beginning of a method? Just thinking out loud. EDIT-> Using the method described in the above paragraph... watch out for comments. For example:

        // This code does such and such (when value is 1)

        Also, watch out for catch, using, and lock statements. -- modified at 15:48 Saturday 24th February, 2007

        G 1 Reply Last reply
        0
        • L Leslie Sanford

          GentooBoxX wrote:

          But every try I make at parsing this, both the methods and constructors gets matched as methods. The problem is that the visibility (public, private) of the method does not have to be there. example: void HelloWorld()

          I've not had much experience with regex, unfortunately, so I can't suggest a regex expression that will work. However, as far as getting false positives with constructures, one way to eliminate them when you find a method is check the method's name. If the name matches that of the class, you can tag it as a constructor. As far as finding methods in the first place, hmm... Wouldn't every line that ends with a right closing parenthesis ')' and is not followed by a semicolone indicate that the line is the beginning of a method? Just thinking out loud. EDIT-> Using the method described in the above paragraph... watch out for comments. For example:

          // This code does such and such (when value is 1)

          Also, watch out for catch, using, and lock statements. -- modified at 15:48 Saturday 24th February, 2007

          G Offline
          G Offline
          Genbox
          wrote on last edited by
          #4

          If i can do this with regex only, thats what i'm going to do. I started this project to get better in regular expressions. Thanks for the suggestion on finding methods, never thought of it that way. But there is one problem with this method, it can be used to find methods, but not extract the name, type and visibility (public, private). I'm currently trying to find every "method" with the same name as the class by using regular expressions, but i must be missing something fundamental, because its not working they way i want it. It possible to reuse capturing groups in regular expressions, but its not working.. yet. this is for finding the class: public class (?\w+)\n*\s*\{ very simple right now, this is the regex extended to reuse the class name: public class (\w+)\n*\s*\{\n*\s*public \w+ \1 But its not working, if anyone have some info on this, please contribute.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups