1
\$\begingroup\$

Following is the string: (I'm getting that from config file so its not constant):

string sequence = "Concat({ACCOUNT_NUM},substring(FormatDate(yyyyMMddHHmmss,DateNow())
,2,12), GetLast(GetNextSequence(seq_relation),1))";

It contains multiple custom methods and I want them somewhere in the same order as they appear in the above string. Following is the strategy I applied:

string[] arbitrary = sequence .Split('(').ToArray();

string[] methodsNmore = arbitrary.Take(arbitrary.Length - 1).ToArray();

string[] array2 = methodsNmore.Where(strr => strr.Contains(',')).ToArray();

string[] methods = array2.Select(str => str.Substring(str.LastIndexOf
                                         (',') + 1, str.Length - str.LastIndexOf
                                         (',') - 1)
                                        ).ToArray();

for (int i = 0; i < methods.Length; i++)
{
   string row = Array.Find(methodsNmore, item => item.Contains(methods[i]));

   int ii = Array.IndexOf(methodsNmore, row);

   methodsNmore[ii] = methods[i];
}

The resulting array, methodsNmore, now contains only the names of methods in the same order as in above string sequence.

Is there any other elegant way of doing it?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can use a regular expression:

string[] names =
  Regex.Matches(sequence, @"([A-Za-z_]\w*)\(").Cast<Match>()
  .Select(m => m.Groups[1].Value).ToArray();
\$\endgroup\$
5
  • \$\begingroup\$ Thanks Dude! I'm not that good at using Regex. Can you refer me the link which is easy to understand and in usage \$\endgroup\$ Commented Jun 16, 2014 at 11:37
  • \$\begingroup\$ @Sadiq: You can see what the parts of the regular expression means here: regex101.com/r/fQ8oK9 \$\endgroup\$ Commented Jun 16, 2014 at 11:45
  • \$\begingroup\$ You even wrote under score in your regex what does it mean. Actually its working without it as well. \$\endgroup\$ Commented Jun 16, 2014 at 11:47
  • \$\begingroup\$ @Sadiq: An underscore is a valid character in an identifer, you could for example have a method named get_last. There are actually more characters that are valid in identifiers, but this covers the characters that are used in english. \$\endgroup\$ Commented Jun 16, 2014 at 11:51
  • \$\begingroup\$ Yes i got it now.. Your link really helped me .. Thanks again :) \$\endgroup\$ Commented Jun 16, 2014 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.