rigamonk
rigamonk

Reputation: 1181

Finding a string literal parameter for a specified function

I have a static class called DataHelper. It has a few methods, but all of them take a string literal (a stored procedure name) as a parameter, like so:

DataHelper.ExecuteProc("DBMaster.DatabaseConnections_SelectAll", null, CONN);

I would like lo go through a source file and extract the string portion of each of these calls. I am currently using:

DataHelper.*(?<=")(?:[^"]|"")*(?=")

but that matches the whole thing. Is there a way via regex to just get the string portion of the parameter list for this function?

Upvotes: 1

Views: 75

Answers (2)

revo
revo

Reputation: 48731

You could use a capturing group or to not have DataHelper.ExecuteProc in matching result put it in lookbehind:

(?<=DataHelper\.ExecuteProc\(")[^\\"]*(?:\\.[^\\"]*)*

See live demo here

Breakdown:

  • (?<= Start of positive lookbehind
    • DataHelper\.ExecuteProc\(" Match it but not consume it
  • ) End of lookbehind
  • [^\\"]*(?:\\.[^\\"]*)* Match string enclosed in double qoutation marks

Upvotes: 2

eocron
eocron

Reputation: 7536

You can do it like this:

var pattern = "\bDataHelper\..+?\(\"(?<procedure>[^\"]*?)\"";
var result = Regex.Match(input, pattern).Cast<Match>().Select(x=> x.Groups["procedure"].Value).ToList();

Upvotes: 1

Related Questions