Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Uppercase Replacement in C#

Tags:

c#

.net

regex

I have the following C# which simply replaces parts of the input string that look like EQUIP:19d005 into URLs, like this:

input = Regex.Replace(input, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);

The HTML ends up looking like this.

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19d005">EQUIP:19d005</a>

The only trouble is that the destination page expects the eqnum querystring to be all UPPERCASE so it returns the correct equipment when eqnum=19D005 but fails if it receives eqnum=19d005.

I guess I can modify and correct EquipmentDisplay.asp's errant requirement of uppercase values however, if possible I'd like to make the C# code comply with the existing classic ASP page by uppercasing the $2 in the Regex.Replace statement above.

Ideally, I'd like the HTML returned to look like this:

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19D005">EQUIP:19d005</a>

Notice although the original string was EQUIP:19d005 (lowercase), only the eqnum= value is uppercased.

Can it be done and if so, what's the tidiest way to do it?

like image 563
Sprogz Avatar asked Oct 15 '08 16:10

Sprogz


3 Answers

OK, 2 solutions, one inline:

input = Regex.Replace(input, @"(EQUIP:)(\S+)", m => string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", m.Groups[1].Value, m.Groups[2].Value, m.Groups[2].Value.ToUpper()), RegexOptions.IgnoreCase);

The other using a separate function:

var input = Regex.Replace(input, @"(EQUIP:)(\S+)", Evaluator, RegexOptions.IgnoreCase);

private static string Evaluator(Match match)
{
    return string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", match.Groups[1].Value, match.Groups[2].Value, match.Groups[2].Value.ToUpper());
}
like image 94
Duncan Avatar answered Oct 05 '22 05:10

Duncan


Using Regex.Replace directly I do not think there is a way. But you could make this a two step process and get the result you are looking for.

var match = Regex.Match(input, @"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
var input = String.Format( @"&lt;a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}""&gt;{0}{1}&lt;/a&gt;", 
match.Groups[1].Value,
match.Groups[2].Value,
match.Groups[2].Value.ToUpper());
like image 37
JaredPar Avatar answered Oct 05 '22 06:10

JaredPar


You can use a MatchEvaluator delegate instead of a string in the replacement. You can then embed the delegate as an anonymous function if on recent .NET. The 'old' solution might look like something like this:

 static void Main(string[] args)
 {
     string input = "EQUIP:12312dd23";
     string output = Regex.Replace(input, @"(EQUIP:)(\S+)", 
         new MatchEvaluator(genURL), RegexOptions.IgnoreCase);
     Console.WriteLine(output);
     Console.ReadKey();
 }
 static string genURL(Match m)
 {
     return string.Format(@"<a title=""View item {0}"" 
            href=""/EqDisp.asp?eq={2}"">{1}{0}</a>",
            m.Groups[2].Value,m.Groups[1].Value,m.Groups[2].Value.ToUpper());
 }
like image 38
Vinko Vrsalovic Avatar answered Oct 05 '22 05:10

Vinko Vrsalovic