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.

My purpouse is to select every character which is surrounded by { and }, this is easily achievable using this regexp {\w*}.

I've developed an extenstion method for strings:

    public static IEnumerable<string> ObtainTokens(this string originalString)
    {
        Regex expression = new Regex(@"{\w*}");
        foreach (Match element in expression.Matches(originalString))
        {
            //Exclude the brackets from the returned valued
            yield return Regex.Replace(element.Value, @"{*}*", "");
        }
    }

Is there a way to get rid of of Regex.Replace? Returning the values as IEnumerable is a good choice?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Modify your regexp: {(\w*)} and then replace:

yield return Regex.Replace(element.Value, @"{*}*", "");

with

yield return element.Groups[1].Value;

ps: full code is avaialbe here

share|improve this answer
    
By applying your suggestion on the test string {THIS} is a {TEST} the response is an IEnumerable<string> containing two empty string. –  Abaco Dec 13 '12 at 9:54
1  
regex has beed updated to capture groups: {(\w*)} –  Akim Dec 13 '12 at 10:16
    
Nice, thank you. –  Abaco Dec 13 '12 at 10:24
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.