0

How can I call 2 functions in my code for one string?

public static string ecleaner(string str)
  {
    return Regex.Replace(str, "[éèê]+", "e", RegexOptions.Compiled);
  }

public static string acleaner(string str)
  {
    return Regex.Replace(str, "[áàâ]+", "a", RegexOptions.Compiled);
  }

Now I want to check the word "Téèááést" ,after this it should look like Teaest .

1
  • 2
    str = acleaner(ecleaner(str)) Commented Mar 20, 2014 at 8:50

3 Answers 3

2

You could use a MatchEvaluator delegate, like this:

public static string cleaner(string str)
{
    return Regex.Replace(str, "(?<a>[áàâ]+)|(?<e>[éèê]+)", onMatch, RegexOptions.Compiled);
}

private static string onMatch(Match m)
{
    if (m.Groups["a"].Success)
        return "a";
    if (m.Groups["e"].Success)
        return "e";

    return "";
}

Or alternatively:

public static string cleaner(string str)
{
    var groups = new[] { "a", "e" };
    return Regex.Replace(str, "(?<a>[áàâ]+)|(?<e>[éèê]+)", m => groups.First(g => m.Groups[g].Success), RegexOptions.Compiled);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Did you try this?

string str = "Téèááést";
str = ecleaner(str);
str = acleaner(str);

Comments

1
    public static class StringExtensions
    {
        public static string ecleaner(this string str)
        {
            return Regex.Replace(str, "[éèê]+", "e", RegexOptions.Compiled);
        }

        public static string acleaner(this string str)
        {
           return Regex.Replace(str, "[áàâ]+", "a", RegexOptions.Compiled);
        }
    }

    //...

    var result = "Téèááést".ecleaner().acleaner();

You could also combine an extension method class with @p.s.w.g's answer, to make things even neater.

Comments

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.