1

I have a string like

string s= "I WAS born in AMERICA on december 1978.";

Now I want to convert december to December using regex

I have used below incomplete regex

s=Regex.Replace(s,(january|february|march|April|may|june|july|august|september|October|november|December),CultreInfo.InvariantCulture.TextInfo.ToTitleCase(?????),RegexOption.IgnoreCase);

What I have to write here (?????) so that I can get below output

s= "I WAS born in AMERICA on December 1978.";

Is there any other way which I can apply??

0

2 Answers 2

2

Looking at regex.replace documentation, I notice that you'd have to use a callback function.

So write a function:

function CustomReplace( Match m ) {
    return CultreInfo.InvariantCulture.TextInfo.ToTitleCase(m.Groups[1].Value)
}

and pass it as the 3rd argument:

s = Regex.Replace(
    s,
    "(january|february|march|April|may|june|july|august|september|October|november|December)",
    CustomReplace,
    RegexOption.IgnoreCase
);
Sign up to request clarification or add additional context in comments.

Comments

0

Using a named capture and inline MatchEvaluator:

string s = "I WAS born in AMERICA on december 1978.";
s = Regex.Replace(
    s,
    "(?<months>january|february|march|April|may|june|july|august|september|October|november|December)",
    new MatchEvaluator(
        match => CultureInfo
                .InvariantCulture
                .TextInfo
                .ToTitleCase(match.Groups["months"].Value)
    ),
    RegexOptions.IgnoreCase
);

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.