Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use a regular expression:

string[] names =
  Regex.Matches(sequence, @"([A-Za-z_]\w*)\(").Cast<Match>()
  .Select(m => m.Groups[1].Value).ToArray();
share|improve this answer
    
Thanks Dude! I'm not that good at using Regex. Can you refer me the link which is easy to understand and in usage –  Sadiq Jun 16 at 11:37
    
@Sadiq: You can see what the parts of the regular expression means here: regex101.com/r/fQ8oK9 –  Guffa Jun 16 at 11:45
    
You even wrote under score in your regex what does it mean. Actually its working without it as well. –  Sadiq Jun 16 at 11:47
    
@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. –  Guffa Jun 16 at 11:51
    
Yes i got it now.. Your link really helped me .. Thanks again :) –  Sadiq Jun 16 at 11:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.