#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Obtaining a line where a match occured


    I have generated a number of RegEx expressions to extract specific items from program source code.
    This is one of the simpler expressions...'^[t\ ]*([a-z][0-9a-z_]*' which extracts labels from the code. Is there a way to establish the line number on which it occurred?

    JohnB
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,829
    Rep Power
    9646
    What language/tool/editor are you working with?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0
    I'm using Delphi Berlin, using TRegEx which is a derivation of the Perl RegEx. I have been developing the expression using the tool from RegExr.com.
    I'm trying to build a record of Vaiables, Labels, Procedures together with the line number each is declared in order to provide quick navigation.
    I have my source text held in a TStringList and I am presenting the text property to my regex expressions. Thats working fine but it obviously doesn't return any indication of where in the text the declaration occurred.
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,829
    Rep Power
    9646
    It's not the prettiest but it will probably be easiest if you loop over the strings and apply the same (compiled) TRegEx to each line.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0
    That is what I was thinking but I was hoping there might be a quicker way just using RegEx.
  10. #6
  11. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,829
    Rep Power
    9646
    Not really. Regular expressions can't track line numbers so you have to do it yourself one way or another. TRegEx might be able to track the character offset of a match (I don't know) but with that you would have to manually count the number of newlines in the string up to that point. But you have a list of strings so calling Text repeatedly isn't a great option. The regex could capture the portion of the string from the beginning to the match but you're still stuck counting newlines - and it won't work if you want to make multiple matches anyways.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0
    Thanks for the help, back to a loop!

IMN logo majestic logo threadwatch logo seochat tools logo