\$\begingroup\$
\$\endgroup\$
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?
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
5
You can use a regular expression:
string[] names =
Regex.Matches(sequence, @"([A-Za-z_]\w*)\(").Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
answered Jun 16, 2014 at 11:05
-
\$\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\$Sadiq– Sadiq2014年06月16日 11:37:13 +00:00Commented 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\$Guffa– Guffa2014年06月16日 11:45:19 +00:00Commented 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\$Sadiq– Sadiq2014年06月16日 11:47:22 +00:00Commented 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\$Guffa– Guffa2014年06月16日 11:51:17 +00:00Commented Jun 16, 2014 at 11:51 -
\$\begingroup\$ Yes i got it now.. Your link really helped me .. Thanks again :) \$\endgroup\$Sadiq– Sadiq2014年06月16日 11:55:58 +00:00Commented Jun 16, 2014 at 11:55
lang-cs