tags:

views:

113

answers:

4

I have a string that looks like this:

var expression = @"Args("token1") + Args("token2")";

I want to retrieve a collection of strings that are enclosed in Args("") in the expression.

How would I do this in C# or VB.NET?

A: 

if your collection looks like this:

 IList<String> expression = new List<String> { "token1", "token2" };


var collection = expression.Select(s => Args(s));

As long as Args returns the same type as the queried collection type this should work okay

you can then iterate over the collection like so

foreach (var s in collection)
{
    Console.WriteLine(s);
}
Michael G
Hi michael,I think you misunderstood the question. My fault on that part, expression is really a string with that value.
cless
Okay, Wasn't sure of what you wanted, I assumed you we're trying to create convert your "Arg()" calls from a list of strings
Michael G
+1  A: 

If you want token1 and token2, you can use following regex

input=@"Args(""token1"") + Args(""token2"")"
MatchCollection matches = Regex.Matches(input,@"Args\(""([^""]+)""\)");

Sorry, If this is not what you are looking for.

S.Mark
No, it's exactly what i'm looking for! thanks a lot!
cless
Forgot to add, is there a non regex solution to my question? Thanks!
cless
Imm.., parsing char by char will be more difficult I think.
S.Mark
Yeah i agree, I was worried at first at the performance of .Net's Regex, so i speculated that a non- Regex solution would be faster... It appears that it's fairly fine...
cless
+3  A: 

Regex:

string expression = "Args(\"token1\") + Args(\"token2\")";
Regex r = new Regex("Args\\(\"([^\"]+)\"\\)");
List<string> tokens = new List<string>();
foreach (var match in r.Matches(expression)) {
    string s = match.ToString();
    int start = s.IndexOf('\"');
    int end = s.LastIndexOf('\"');
    tokens.add(s.Substring(start + 1, end - start - 1));
}

Non-regex (this assumes that the string in the correct format!):

string expression = "Args(\"token1\") + Args(\"token2\")";
List<string> tokens = new List<string>();
int index;
while (!String.IsNullOrEmpty(expression) && (index = expression.IndexOf("Args(\"")) >= 0) {
    int start = expression.IndexOf('\"', index);
    string s = expression.Substring(start + 1);
    int end = s.IndexOf("\")");
    tokens.Add(s.Substring(0, end));
    expression = s.Substring(end + 2);
}
Jason
+1  A: 

There is another regular expression method for accomplishing this, using lookahead and lookbehind assertions:

    Regex regex = new Regex("(?<=Args\\(\").*?(?=\"\\))");

    string input = "Args(\"token1\") + Args(\"token2\")";

    MatchCollection matches = regex.Matches(input);

    foreach (var match in matches)
    {
        Console.WriteLine(match.ToString());
    }

This strips away the Args sections of the string, giving just the tokens.

David Hall
Thanks a lot! In S. Mark's solution, i had to perform extraction of the token...
cless