1

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?

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

3 Comments

Forgot to add, is there a non regex solution to my question? Thanks!
Imm.., parsing char by char will be more difficult I think.
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...
0

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);
}

2 Comments

Hi michael, I think you misunderstood the question. My fault on that part, expression is really a string with that value.
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

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.