tags:

views:

3716

answers:

6

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?

A: 

Assuming that input is a string:

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

Changing the case of a string isn't something regex does.

John Fiala
+3  A: 

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);
            input = 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());
JaredPar
+1  A: 

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());
 }
Vinko Vrsalovic
+5  A: 

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:

    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());
}
Duncan
The one-liner is a very elegant solution, which worked great by the way. @Vinko mentions using an anonymous function but didn't give an example, hence why I chose your solution. I'm a bit confused as I've seen => used for LINQ to SQL, expression trees and now anonymous functions? Why so many uses?
Sprogz
The => style syntax is a shorter version of delegate() { } syntax. The other advantages are if you have a one liner like above you don't need a return statement and the types are inferred - so I just specify m instead of Match m. The lambda syntax helps make code more succinct.
Duncan
A: 
string input = "EQUIP:19d005";
Regex regex = new Regex (@"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
string eqlabel = regex.Replace(input, "$1");
string eqnum = regex.Replace(input, "$2");
string eqnumu = eqnum.ToUpperInvariant();
input = string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", eqlabel, eqnum, eqnumu);
Jesse C. Slicer
A: 
public static string FormatToCustomAnchorTag(this string value)
{

    return Regex.Replace(value.ToLower() + value.ToUpper(),
       @"(?<equiplo>equip:)(?<equipnolo>\S+)(?<equipup>EQUIP:)(?<equipnoup>\S+)",
       @"<a title=""View equipment item ${equipnolo}"" href=""/EquipmentDisplay.asp?eqnum=${equipnoup}"">${equipup}${equipnolo}</a>");
}
Vivek