Andreas
Andreas

Reputation: 1479

Remove a character when between two numbers (regular expression)

I have a date with weeks using this format:

2012-15 - 2012-20

I want to format it to look like

2012 v15 - 2012 v20

How can this be done c#?

Upvotes: 0

Views: 693

Answers (3)

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Try this:

string input = "2012-15 - 2012-20";
string output = Regex.Replace(input, @"(\d{4})-(\d+)", "$1 v$2");

Upvotes: 1

Shai
Shai

Reputation: 25619

string input = "2012-15 - 2012-20";
string output = Regex.Replace(input, @"\b(-)\b", "v");

Upvotes: 0

npinti
npinti

Reputation: 52185

This should do the trick:

        String str = "2012-15 - 2012-20";
        String newStr = Regex.Replace(str, "(\\d+)-(\\d+)", "$1 v$2");
        Console.WriteLine(str);
        Console.WriteLine(newStr);
        Console.ReadLine();

Prints Out:

2012-15 - 2012-20

2012 v15 - 2012 v20

Upvotes: 3

Related Questions